Posts Tagged String

Java – String trim() Example, Strings trim() program

Sunday, September 27th, 2009

Whitespaces in String can be removed with the help of trim() method in java. trim() method returns a value in String as a copy of string. Trim method remove whitespace from starting and end of string only, it will not remove whitespaces from inside of string. Use of String trim() in java is shown below.

Example of String trim() in java, JSP

public class StringTrimExample {

    public static void main(String[] args) {

        String string = " Java String ";

        String str = string.trim();

        System.out.println("Output without trim :"+string);

        System.out.println("Output with trim :"+str);
    }
}

Output

Output without trim : Java String
Output with trim :Java String