Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java trim() Method

Java trim() Method

Java String ClassJava String class


The trim() method is used to remove leading and trailing whitespace from the string.

Syntax


public String trim()

Parameters

Return Value

The string with leading and trailing whitespace removed.

Example


public class Test {
    public static void main(String args[]) {
        String Str = new String("    www.runoob.com    ");
        System.out.print("原始值 :" );
        System.out.println( Str );

        System.out.print("删除头尾空白 :" );
        System.out.println( Str.trim() );
    }
}

The execution result of the above program is:

原始值 :    www.runoob.com    
删除头尾空白 :www.runoob.com

Java String ClassJava String class