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
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ adding a newline character to a string in java
Adding a Newline Character to a String in Java | Baeldung
May 11, 2024 - Adding a new line in Java is as simple as including โ€œ\nโ€ , โ€œ\rโ€, or โ€œ\r\nโ€ at the end of our string.
People also ask

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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-print-a-new-line-in-string
Java Program to Print a New Line in String - GeeksforGeeks
June 6, 2021 - Note: Here the new line character is "\n" for Unix and "\r\n" for Windows OS. ... // Java program to print a new line in string // Using platform-dependent Newline Character // Main class class GFG { // Main Driver Code public static void ...
๐ŸŒ
SSOJet
ssojet.com โ€บ dev guides โ€บ special characters โ€บ newline (\n) in java
Newline (\n) in Java | Developer Guides
September 6, 2025 - You can directly embed newline characters within Java string literals using the \n escape sequence. This is the standard and most efficient way to force a line break in your programโ€™s output.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ how to get a new line character in java?
How to get a new line character in Java?
October 11, 2023 - For example, using the keyword โ€œintโ€ will make the datatype of a variable an integer. Likewise, using โ€œ\nโ€ or โ€œ\r\nโ€ while printing any String in Java, a โ€œbackslash nโ€ wonโ€™t be printed. Instead, a new line break will be printed ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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(); } } }
๐ŸŒ
STechies
stechies.com โ€บ newline-string-java
How to Add a Newline to a String in Java?
A Newline character is a particular type of character literal or an escape sequence in a Java program, representing the end of a line and the beginning of a line. In programming languages like C, C++, Java, etc., programmers use the '\n' to make ...
๐ŸŒ
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.
๐ŸŒ
xperti
xperti.io โ€บ home โ€บ adding a newline character to a string in java
How to add a new line to a String in Java?
April 28, 2022 - It can be done in Java by adding newline characters to a string. A newline character, also known as the end of line (EOL), line break or line separator is a control character used to represent the end of a line and the beginning of a new one, separating both of them.
๐ŸŒ
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) { ...
๐ŸŒ
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); } }
๐ŸŒ
Quora
quora.com โ€บ How-do-you-create-a-new-line-in-Java-programming
How to create a new line in Java programming - Quora
Answer (1 of 6): There are a couple of ways using which this might be achievable: 1. [code ]System.out.println();[/code] [takes care of the newline implicitly] 2. Using a combination of escape sequences: 2.1. [code ]System.out.print("\n");[/code] 2.2. [code ]System.out.print("\n\r");...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-adding-a-newline-character-to-a-string-117387
Java | New Line Character | Programming Tutorials | LabEx
In this step, we will use the platform-independent newline character %n to add a new line. Add the following code to the NewLineCharacter.java file:
๐ŸŒ
TechBloat
techbloat.com โ€บ home โ€บ how to do a new line in java
How to Do a New Line in Java - TechBloat
April 16, 2026 - Java provides multiple ways to insert new lines, but the most common method leverages the newline character. The newline character is represented as \n within string literals and is interpreted as a command to move to the next line when the string is printed or written.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ how to append a newline to a stringbuilder
How to Append a Newline to a StringBuilder | Baeldung
September 25, 2024 - To learn more about line break types and their implications, check out the Difference Between CR LF, LF, and CR Line Break Types. The following code used โ€œ\nโ€ to add a new line in StringBuilder: