Freigeben über


String.trimStart Function

Removes leading white-space characters from a String object.

var trimmedStringVar = myString.trimStart();

Return Value

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

Remarks

Use the trimStart function to remove leading 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 trimStart function to remove leading white space from a current 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">
            // Determines if a string has a specific prefix as 
            // the first non white-space characters in a string.
            function verifyString(myString, prefix) 
            {
                // Remove any white space at the left of the string.
                myString = myString.trimStart();

                // Set to lower case.
                myString = myString.toLowerCase();

                // Determine if the string starts with the specified prefix.
                var hasPrefix = myString.startsWith(prefix.toString());
                if (hasPrefix === true)
                {
                    alert("The string \"" + myString + "\" starts with \"" + prefix + "\""); 
                }
                else
                {
                   alert("The string \"" + myString + " does not start with \"" + prefix + "\""); 
                }
             }

            // Displays: The string "green_blue_red" starts with "green"
            verifyString("  GREEN_BLUE_RED  ", "green");

        </script>
    </form>
</body>
</html>

See Also

Reference

String.trimEnd Function

Other Resources

Language Reference