Java trim() Method

Java String类Java String Class


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

Syntax

public String trim()

Parameters

  • None

Return Value

A string with leading and trailing whitespace removed.

Example

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

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

The above program produces the following result:

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

Java String类Java String Class

Other Extensions