String.trimEnd 函数

更新:2007 年 11 月

String 对象移除尾随空白字符。

var trimmedStringVar = myString.trimEnd();

返回值

字符串的一个副本,其中移除了该字符串末尾的所有空白字符。

备注

使用 trimEnd 函数可以从 String 对象移除尾随空白字符。空格和制表符都属于空白字符。

示例

下面的代码示例演示如何使用 trimEnd 函数从 String 对象移除尾随空白。

<!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>Untitled Page</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 specified suffix as 
            // the last non white-space characters regardless of case.
            function verifyStringSuffix(myString, suffix) 
            {
                // Remove any trailing white spaces.
                myString = myString.trimEnd();

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

                // Determine if the string ends with the specified suffix.
                var isTxt = myString.endsWith(suffix.toString());

                if (isTxt === true)
                {
                    alert("The string \"" + myString + "\" ends with \"" + suffix + "\""); 
                }
                else
                {
                   alert("The string \"" + myString + "\" does not end with \"" + suffix + "\""); 
                }
            }

            verifyStringSuffix("some_file.TXT  ", ".txt");

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


请参见

参考

String.trimStart 函数

其他资源

语言参考