String 类型扩展

更新:2007 年 11 月

通过静态方法和实例方法,提供对基本 ECMAScript (JavaScript) String 对象的扩展。

**命名空间:**无。此类型扩展是全局性的,不属于任何命名空间。

继承:String

var stringVar = new String();

成员扩展

名称

说明

String.endsWith 函数

确定 String 对象的末尾是否与指定的字符串匹配。

String.format 函数

String 对象中的每个格式项替换为相应对象值的文本等效项。

String.localeFormat 函数

String 对象中的格式项替换为相应对象值的文本等效项。使用当前区域性设置日期和数字的格式。

String.startsWith 函数

确定 String 对象的开头是否与指定的字符串匹配。

String.trim 函数

String 对象实例移除前导空白字符和尾随空白字符。

String.trimEnd 函数

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

String.trimStart 函数

String 对象实例移除前导空白字符。

备注

String 扩展是 Microsoft AJAX Library 的一部分。这些扩展在 JavaScript String 对象中添加了功能,并提供了 .NET 编程人员更熟悉的成员。

有关此类型扩展的 JavaScript 对象及其构造函数的更多信息,请参见 语言参考中的 String 对象

示例

下面的示例演示如何创建 String 对象的实例,以及如何调用 Microsoft AJAX LibraryString.format 函数将 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>Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager1">
        </asp:ScriptManager>

        <script type="text/javascript">
                function runExample() 
                {
                    var newStr = new String("       This is a string   ");
                    // Remove the white space at the beginning and end.
                    var trimmedStr = newStr.trim();
                    // Displays: "The string with white spaces removed:This is a string."
                    alert("The string with white spaces removed:" + trimmedStr + ".");  
                }
                runExample();
        </script>
    </form>
</body>
</html>

请参见

参考

Array 对象

new 运算符

String 类型函数