JavaScript

String method trim()


Strips leading and trailing whitespace from the string, and returns it.

Synopsis

 var s = str.trim();

Returns

A new string, with the whitespace characters at the beginning and end of the string omitted.


Description

The trim method works exactly like the trim method of the java.lang.String class in Java. For more information, see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#trim().


Sample

var s0 = ""; var s1 = "Hello World!"; var s2 = "\n\n\n\rHello World!"; var s3 = "Hello World!\n\r\n"; var s4 = " Hello World! "; var s5 = "\r \r \rHello World! \r \t\n"; var s6 = "\b\t\v\n\r "; var s7 = "\tHello \t World!\t".trim(); s0 = s0.trim(); s1 = s1.trim(); s2 = s2.trim(); s3 = s3.trim(); s4 = s4.trim(); s5 = s5.trim(); s6 = s6.trim(); var str = String.format("s0=%S\ns1=%S\ns2=%S\ns3=%S\ns4=%S\ns5=%S\ns6=%S\ns7=%S\n",s0,s1,s2,s3,s4,s5,s6,s7); document.getElementById("textarea").value = str;

Click the button below to run the test.


Alex Eng,