Java Program to Print a New Line in String
Last Updated :
06 Jun, 2021
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
// 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");
}
}
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
// 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");
}
}
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
// 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
// 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");
}
}
Method-5: Using System.out.println() method.
Example
Java
// 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");
}
}
Similar Reads
Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a
4 min read
Java Program to Read a File to String There are multiple ways of writing and reading a text file. This is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Given a text file, the task is to read the contents
8 min read
Java Program to Replace All Line Breaks from Strings Given a string, write a Java program to replace all line breaks from the given String. Examples Input - Geeks For Geeks Output - Geeks For Geeks Line Break: A line break ("\n") is a single character that defines the line change. In order to replace all line breaks from strings replace() function can
2 min read
How to Pad a String in Java? Padding a String in Java involves adding extra characters, which may be spaces, zeros, or other given characters to the beginning or end of a string to meet a desired length. This is normally used in formatting outputs or aligning texts.Example:The String.format() method is a simple way to pad strin
3 min read
Java Program to Save a String to a File A demo file on the desktop named 'gfg.txt' is used for reference as a local directory on the machine. Creating an empty file before writing a program and give that specific path of that file to the program. Methods: Using writeString() method of Files classUsing write() method of Files classUsing wr
6 min read
Reverse a String in Java In Java, reversing a string means reordering the string characters from the last to the first position. Reversing a string is a common task in many Java applications and can be achieved using different approaches. In this article, we will discuss multiple approaches to reverse a string in Java with
7 min read