且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Microsoft AJAX Library对 String的扩展

更新时间:2022-09-16 19:21:18

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

String.startsWith 函数

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

使用 startsWith 函数可确定 String 对象的开头部分是否与指定的字符串匹配。 startsWith 函数区分大小写。

Microsoft AJAX Library对 String的扩展
/* param
prefix:要与 String 对象的开头部分进行匹配的字符串

return:如果 String 对象的开头部分与 prefix 匹配,则该值为 true;否则为 false
*/
var hasPrefix = myString.startsWith(prefix);
Microsoft AJAX Library对 String的扩展

String.endsWith 函数

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

使用 endsWith 函数可确定 String 对象的末尾是否与指定的字符串匹配。 endsWith 函数区分大小写。

Microsoft AJAX Library对 String的扩展
/* param
suffix:要与 String 对象的末尾进行匹配的字符串。

return:如果 String 对象的末尾与 suffix 匹配,则为 true;否则为 false。
*/
var hasSuffixVar = myString.endsWith(suffix);
Microsoft AJAX Library对 String的扩展

String.trim 函数

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

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

/* param
return:一个字符串副本,其中从该字符串的开头和末尾移除了所有空白字符
*/
var trimmedStringVar = myString.trim();

与该函数功能相似的还有:String.trimStart 函数 和 String.trimEnd 函数

示例代码:

Microsoft AJAX Library对 String的扩展
<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>
Microsoft AJAX Library对 String的扩展

String.format 函数

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

Microsoft AJAX Library对 String的扩展
/* param
format:格式字符串
args:要设置其格式的对象的数组

return:具有所应用格式设置的字符串副本
*/
var s = String.format(format, args);
Microsoft AJAX Library对 String的扩展

使用 format 函数可以用相应对象值的文本表示形式替换指定的格式项。 args 参数可以包含单个对象或对象数组。 format 参数由零个或多个固定文本序列与一个或多个格式项混和组成。每个格式项都对应于 objects 中的一个对象。在运行时,每个格式项都由列表中相应对象的字符串表示形式替换。

格式项包含一个用大括号括起来的编号(如 {0}),该编号标识 objects 列表中的一个相应项。编号从零开始。若要在 format 中指定单个大括号字符,请指定两个前导或尾随大括号字符,即“{{”或“}}”。不支持嵌套大括号。

通过在 args 参数中提供一个具有 toFormattedString 方法的特殊格式设置对象,可以为格式项指定自定义格式设置例程。 toFormattedString 方法必须接受一个字符串参数并返回一个字符串。在运行时,format 函数将括在其相应参数说明符的大括号中的任何字符串都传递给 toFormattedStrings 方法。 toFormattedString 方法返回的字符串将插入到格式化字符串中相应参数说明符的位置处。

示例代码:

Microsoft AJAX Library对 String的扩展
// Define an class with a custom toFormattedString
// formatting routine.
Type.registerNamespace('Samples');

Samples.ToFormattedStringExample = function() {
}
Samples.ToFormattedStringExample.prototype = {
    toFormattedString: function(format) {
        return "This was custom formatted: " + format;
    }
}
Samples.ToFormattedStringExample.registerClass('Samples.ToFormattedStringExample');

var result = "";

// Format a string.
result = String.format("{0:d}", 123);
// Displays: "123"
alert(result);

// Format a string with an object that has 
// a custom toFormattedString method.
var o = new Samples.ToFormattedStringExample();
result = String.format("{0:12345}", o);
// Displays: "This was custom formatted: 12345"
alert(result);


本文转自齐师傅博客园博客,原文链接:http://www.cnblogs.com/youring2/archive/2013/01/06/2847863.html,如需转载请自行联系原作者