String.trim Function
Removes leading and trailing white-space characters from a String object.
var trimmedStringVar = myString.trim();
The following example shows how to use the trim function to remove leading and trailing white space from a String object.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Sample</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager runat="server" ID="ScriptManager1"> </asp:ScriptManager> <script type="text/javascript"> function runSample() { var myString = " A string "; // Remove any leading and trailing white spaces. myString = myString.trim(); alert("Test:" + myString + "."); } // Displays: "Test:A string." runSample(); </script> </form> </body> </html>
Show: