How to add a newline character to a string in Java

In this tutorial, we will learn how to add a newline character to a string in Java. Java language provides enormous features to make our tasks much easier. Strings in java also include a lot of features facilitating the manipulation of strings in different ways. One of the features is adding a newline character to the string. There are many ways in which we can add a newline character to a string. They are:

  1. System.out.println()
  2. %n(newline character)
  3. \n( A platform-dependent newline character)
  4. System.lineSeperator()
  5. System.getProperty()

Ways to Add a newline character to a string in Java

We will explain each method one by one with examples.

System.out.println():

This is the most easiest and efficient way to add a newline character in a string. We can change the line by writing System.out.println() and provide the content in the parenthesis in Java.

Code:

class Main {
    
    public static void main(String[] args)
    {
        System.out.println("newline");
        System.out.println("character");
    }
}

Output:

newline
character

%n (newline character):

We can also use this method for changing the line of the string. In this method, we have to add this %n in between the string in which we have to have to change the line.

Code:

class Main {

    public static void main(String[] args)
    {
         
        System.out.printf("newline%ncharacter");
    }
}

Output:

newline
character

\n in Java (A platform-dependent newline character):

This is also the most commonly used and most favorable way to add a newline character to the string. This is included within a single quote in between the strings in which we want to change the line:

Code:

import java.util.*;
class Main {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String s1,s2;
        System.out.println("Enter string 1: ");
        s1 = in.nextLine();
        System.out.println("Enter string 2: ");
        s2 = in.nextLine();
        System.out.println(s1 + '\n' + s2);
    }
}

Output:

Enter string 1: 
newline
Enter string 2: 
character
After adding a newline character: 
newline
character

System.lineSeperator()

It is a method that returns a line separator string. This returned value is system dependent. The most important point is that it returns the same value every time. This method can also be used to change the line of the string in java.

Code:

import java.util.*;
class Main {
    
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String s1,s2;
        System.out.println("Enter string 1: ");
        s1 = in.nextLine();
        System.out.println("Enter string 2: ");
        s2 = in.nextLine();
        String newline = System.lineSeparator();
        System.out.println("After using line separator() method: ");
         System.out.println(s1 + newline + s2);
    }
}

Output:

Enter string 1: 
newline
Enter string 2: 
character
After using line separator() method: 
newline
character

System.getProperty():

It is also a method that returns a line separator string. This is also system dependent. But here it takes the parameter for the value of the system property. We can also change the line in the strings using this method.

Code:

import java.util.*;
class Main {
    
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String s1,s2;
        System.out.println("Enter string 1: ");
        s1 = in.nextLine();
        System.out.println("Enter string 2: ");
        s2 = in.nextLine();
        String newline = System.getProperty("line.separator");
        System.out.println("After using getProperty() method: ");
         System.out.println(s1 + newline + s2);
    }
}

Output:

Enter string 1: 
newline
Enter string 2: 
character
After using getProperty() method: 
newline
character

Hope this tutorial was useful.

Also read: How to write multi-line comments in Java

Leave a Reply

Your email address will not be published.