Write a Java program to trim any leading or trailing whitespace from a given string.

Java Programming Language String in java (Article) String in java (Program)

1629

Program:

public class Exercise {

public static void main(String[] args)
    {
        String str = " Java Exercises ";

        // Trim the whitespace from the front and back of the
        // String.
        String new_str = str.trim();

        // Display the strings for comparison.
System.out.println("Original String: " + str);
System.out.println("New String: " + new_str);
    }
}

Output:

Original String:  Java Exercises
New String: Java Exercises
Press any key to continue . . .

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.