zoukankan      html  css  js  c++  java
  • Microsoft AJAX Library对 String的扩展 拓荒者

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

    String.startsWith 函数

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

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

    /* param
    prefix:要与 String 对象的开头部分进行匹配的字符串
    
    return:如果 String 对象的开头部分与 prefix 匹配,则该值为 true;否则为 false
    */
    var hasPrefix = myString.startsWith(prefix);

    String.endsWith 函数

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

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

    /* param
    suffix:要与 String 对象的末尾进行匹配的字符串。
    
    return:如果 String 对象的末尾与 suffix 匹配,则为 true;否则为 false。
    */
    var hasSuffixVar = myString.endsWith(suffix);

    String.trim 函数

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

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

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

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

    示例代码:

    <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>

    String.format 函数

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

    /* param
    format:格式字符串
    args:要设置其格式的对象的数组
    
    return:具有所应用格式设置的字符串副本
    */
    var s = String.format(format, args);

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

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

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

    示例代码:

    // 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);
  • 相关阅读:
    C#的泛型的类型参数可以有带参数的构造函数的约束方式吗
    Object Pool
    链表排序(冒泡、选择、插入、快排、归并、希尔、堆排序)
    ASP.NET MVC应用程序使用axd格式文件
    框架设计--服务总线
    Mylyn
    不要问我能赚多少,当你问这句话的时候,你的思想还停留在给别人打工的阶段,我只是你的仓库,能挣多少得问你自己想挣多少(转)
    maven中如果使用本地jar
    君子和而不同,小人同而不和
    失去控制,并随之失去指挥
  • 原文地址:https://www.cnblogs.com/youring2/p/2847863.html
Copyright © 2011-2022 走看看