String trim example in java

The trim method of String class returns a copy of the string, with leading and trailing whitespace omitted.


package com.java.connect.string;
 
// String trim example in java
public class StringTrimExamle {
 
    public static void main(String[] args) {
        //
        // declare the String with some text
        //
        String string = " Some text here ";
 
        //
        // To remove leading and trailing space from string.
        // You need to use, public String trim() method of String class as shown below.
        //
 
        String trimmed = string.trim();
 
        System.out.println("The original string is: " + string);
        System.out.println("The String after removing sapce is : " + trimmed);
    }
}

The output of the above example will be as:

The original string is:  Some text here 
The String after removing sapce is : Some text here