Java trim() Method
The trim() method is used to remove leading and trailing whitespace from the string.
Syntax
public String trim()
Parameters
-
None
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 class