Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, that is how to print a new line in a string using Java.
Methods:
There are many ways to print new line in string been illustrated as below:
- Using System.lineSeparator() method
- Using platform-dependent newline character
- Using System.getProperty() method
- Using %n newline character
- Using System.out.println() method
Let us discuss them individually in detail.
Method 1: Using System.lineSeparator() method
Example
// Java program to print a new line in string // Using System.lineSeparator() method // Main class class GFG {
// Main Driver Code
public static void main(String[] args)
{
// Calling the System.lineSeparator() function to
// print newline in between some specified strings
String newline = System.lineSeparator();
// Printing new line
System.out.println( "GFG" + newline + "gfg" );
}
} |
GFG gfg
Method 2: Using platform-dependent newline character.
Note: Here the new line character is “\n” for Unix and “\r\n” for Windows OS.
Example
// Java program to print a new line in string // Using platform-dependent Newline Character // Main class class GFG {
// Main Driver Code
public static void main(String[] args)
{
// Using new line Character '\n' to print
// new line in between strings
System.out.println( "GFG" + '\n' + "gfg" );
}
} |
GFG gfg
Method 3: Using System.getProperty() method. Here this function uses the value of the system property “line.separator”, which returns the system-dependent line separator string.
Example
// Java program to print a new line in string class GFG
{ // Main Driver Code
public static void main(String[] args)
{
// Calling System.getProperty() function Over The
// parameter for the value of the system property "line.separator",
// which returns the system-dependent line separator string.
String newline = System.getProperty( "line.separator" );
// Printing new line between two strings
System.out.println( "GFG" + newline + "gfg" );
}
} |
Output:
GFG gfg
Method 4: Using %n newline character
Note: Here this newline character is used along with the printf() function.
Example
// Java program to print a new line in string // Using %n Newline Character // Importing input output classes import java.io.*;
// Main class class GFG {
// Main Driver Code
public static void main(String[] args)
{
// Printing new line using new line
// Character "%n" with the printf() method
System.out.printf( "GFG%ngfg" );
}
} |
GFG gfg
Method-5: Using System.out.println() method.
Example
// Java program to print a new line in string // Using System.out.println() method // Main class class GFG {
// Main driver method
public static void main(String[] args)
{
// Printing new line using
// System.out.println() function
// over custom string inputs
System.out.println( "GFG" );
System.out.println( "gfg" );
}
} |
GFG gfg