Java is the most powerful programming language we can use to multitask, and Java is the language of choice in the industry. So it is filled with many functions. Here we will discuss one of the best features of Java, which is how to use Java to print newlines in a string.
Methods:
There are many ways to print new line in string been illustrated as below:
Let us discuss them individually in detail.
Method 1: Using the 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"); } }
Output
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"); } }
Output
GFG gfg
Method 3: Using the System.getProperty() method. This function uses the system property value "line.separator", which returns a string that the system relies on to separate the lines.
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"); } }
Output
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"); } }
Output
GFG gfg
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.