Freigeben über


String.trim Function

Removes leading and trailing white-space characters from a String object.

var trimmedStringVar = myString.trim();

Return Value

A copy of the string with all white-space characters removed from the start and end of the string.

Remarks

Use the trim function to remove leading and trailing white-space characters from the current String object. Examples of white-space characters are spaces and tabs.

Example

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" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://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>

See Also

Other Resources

Language Reference