Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

In Java How to add New line in String?

In Java How to add New line in String?

In this tutorial we will go over two different ways to add new line to any String object.

Let’s get started:

  • Create class CrunchifyAddNewLineToString.java
  • Copy below code and save file
package crunchify.com.java.tutorials;

/**
 * @author Crunchify.com
 * Program: In Java How to add New Line in String?
 */

public class CrunchifyAddNewLineToString {

    public static void main(String[] args) {

        // Method-1: Using \n line separator
        System.out.println("===================== Method-1 ====================");
        String crunchifyText = "ORIGINAL: \n" + "Crunchify is the largest free, premium, technical & blogging resource site for beginners, who are passionate & have the desire to excel in the online world.";

        System.out.println(crunchifyText);

        // Simple way to add  new line:
        // \n for MacOS
        // \r\n for Windows
        String crunchifyUpdatedText = "\nCrunchify is the largest free, premium, \ntechnical & blogging resource site for beginners, \nwho are passionate & have the desire to excel in the online world.";
        System.out.println(crunchifyUpdatedText);


        // Method-2: Using System.lineSeparator()
        System.out.println("\n\n===================== Method-2 ====================");
        String crunchifyText2 = "ORIGINAL: \n" + "Crunchify is the largest free, premium, technical & blogging resource site for beginners, who are passionate & have the desire to excel in the online world.";

        System.out.println(crunchifyText2);

        // System: The System class contains several useful class fields and methods.
        // It cannot be instantiated.
        // Among the facilities provided by the System class are standard input, standard output,
        // and error output streams; access to externally defined properties and environment variables;
        // a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

        // lineSeparator(): Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator.
        // On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".
        String crunchifyUpdatedText2 = System.lineSeparator() +
                "Crunchify is the largest free, premium, "
                + System.lineSeparator() + "technical & blogging resource site for beginners,"
                + System.lineSeparator() + "who are passionate & have the desire to excel in the online world.";

        // println: Prints a String and then terminate the line.
        // This method behaves as though it invokes print(String) and then println().
        System.out.println(crunchifyUpdatedText2);
    }

}

IntelliJ IDEA console result:

Just run above program as a Java program and you will result as below.

===================== Method-1 ====================
ORIGINAL: 
Crunchify is the largest free, premium, technical & blogging resource site for beginners, who are passionate & have the desire to excel in the online world.

Crunchify is the largest free, premium, 
technical & blogging resource site for beginners, 
who are passionate & have the desire to excel in the online world.


===================== Method-2 ====================
ORIGINAL: 
Crunchify is the largest free, premium, technical & blogging resource site for beginners, who are passionate & have the desire to excel in the online world.

Crunchify is the largest free, premium, 
technical & blogging resource site for beginners,
who are passionate & have the desire to excel in the online world.

Process finished with exit code 0

Let me know if you face any issue running this Java program.

The post In Java How to add New line in String? appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires