System.out.println("I\nam\na\nboy");
System.out.println("I am a boy".replaceAll("\\s+","\n"));
System.out.println("I am a boy".replaceAll("\\s+",System.getProperty("line.separator"))); // portable way
Answer from Prince John Wesley on Stack Overflow Top answer 1 of 16
161
System.out.println("I\nam\na\nboy");
System.out.println("I am a boy".replaceAll("\\s+","\n"));
System.out.println("I am a boy".replaceAll("\\s+",System.getProperty("line.separator"))); // portable way
2 of 16
154
You can also use System.lineSeparator():
String x = "Hello," + System.lineSeparator() + "there";
03:02
Beginning Java Tutorial: 5 New Line and Tab escape characters - ...
05:01
Java Escape Characters - Newline Backslash Single and Double Quote ...
00:48
"\n" vs. "%n": line separators #java #shorts
04:48
How to print each word in new line in Java? | StringTokenizer - ...
00:48
"\n" vs. "%n": line separators #java #shorts - YouTube
06:52
Using the Java Scanner Class - next, nextInt, nextDouble & nextLine ...
How do I add a new line in a Java string?
Put \n in the literal ("line1\nline2"), concatenate System.lineSeparator(), or use a text block with """ for multiline text.
mojoauth.com
mojoauth.com โบ dev guides โบ \n in java: what it does and new line examples
\n in Java: What It Does and New Line Examples | MojoAuth Dev Guides
What is \n in Java?
\n is Java's escape sequence for the newline (line feed) character, code 10. Inside a string it breaks the output onto a new line when printed.
mojoauth.com
mojoauth.com โบ dev guides โบ \n in java: what it does and new line examples
\n in Java: What It Does and New Line Examples | MojoAuth Dev Guides
What is %n in Java?
%n is a format specifier for printf and String.format that outputs the platform line separator: \n on Linux and macOS, \r\n on Windows.
mojoauth.com
mojoauth.com โบ dev guides โบ \n in java: what it does and new line examples
\n in Java: What It Does and New Line Examples | MojoAuth Dev Guides
Mkyong
mkyong.com โบ home โบ java โบ java โ how to get a new line character or \n?
Java โ How to get a new line character or \n? | mkyong.com
July 21, 2020 - package com.mkyong.io.howto; public class GetNewLine2 { public static void main(String[] args) { System.out.print(String.format("%s%n%s", "Line 1", "Line 2")); } } ... If we use BufferedWriter to write text to a file, the newLine() represents the platform-dependent newline character. ... package com.mkyong.io.howto; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.List; public class GetNewLine3 { public static void main(String[] args) { // write a new line character String fileName = "/home/mkyong/sample.txt"; List<String> content = Arrays.asList("A", "B", "C"); try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) { for (String t : content) { bw.write(t); bw.newLine(); // same //bw.write(System.lineSeparator()); } } catch (IOException e) { e.printStackTrace(); } } }
Java Code Geeks
examples.javacodegeeks.com โบ home โบ java development โบ core java
Java New Line Character Example - Examples Java Code Geeks - 2026
August 25, 2020 - According to the ASCII table, there are two control characters which represent it: ... Java defines escape sequence of "\n" for LF and "\r" for CR. Each Operating System (OS) treats the new line character differently.
Java2Blog
java2blog.com โบ home โบ core java โบ string โบ character โบ new line character in java
New line character in java - Java2Blog
May 9, 2021 - We can use System.lineSeparator() to separate line in java. It will work in all operating systems. You can also use System.getProperty("line.separator") to put new line character in String.
Dot Net Perls
dotnetperls.com โบ newline-java
Java - Newline Examples: System.lineSeparator - Dot Net Perls
We added newlines to strings with concatenation. We use System.lineSeparator, a Character constant, and the newline literal.
Java Lessons
javalessons.com โบ home โบ blog โบ adding newline characters in java: comprehensive guide
Java New Line Characters: Formatting Text Output
October 9, 2023 - In Java, you can achieve this by adding newline characters to a string. A newline character, also known as the end-of-line (EOL) character, line break, or line separator, is a control character used to signify the end of a line and the start of a new one, effectively separating them.
Mkyong
mkyong.com โบ home โบ java โบ java โ add new line in string
Java โ Add new line in String | mkyong.com
May 6, 2019 - package com.mkyong; public class NewLineExample2 { public static void main(String[] args) { String original = "Hello World Java"; System.out.println(original); String originalNewLine = "Hello" + System.lineSeparator() + "World" + System.lineSeparator() + "Java"; System.out.println(originalNewLine); } }
Educative
educative.io โบ answers โบ how-to-get-and-use-a-new-line-character-in-java
How to get and use a new line character in Java
We can use the control characters, \n in Unix and \r\n in Windows, as part of strings to denote the newline. The problem with this approach would be the portability of the program. public class Main { public static void main(String[] args) { ...
Coderanch
coderanch.com โบ t โบ 411039 โบ java โบ insert-line-character-String
How to insert a new line character in a String? (Beginning Java forum at Coderanch)
A new line character can be inserted in a string by using an escape sequence. The most commonly used escape sequence for a new line is \n. When \n is placed inside a string, it indicates that the text following it should start on a new line. This is widely supported in programming languages ...