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

String.startsWith 函数

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

 startsWith 函数区分大小写。

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

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

 

String.endsWith 函数

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

 endsWith 函数区分大小写。

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

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

 

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

在运行时,每个格式项都由列表中相应对象的字符串表示形式替换。

不支持嵌套大括号。

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

 

 

 

相关文章:

  • 2022-12-23
  • 2021-06-15
  • 2021-07-01
  • 2021-05-27
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2022-02-27
猜你喜欢
  • 2021-06-20
  • 2022-01-02
  • 2021-08-24
  • 2021-08-23
  • 2022-01-26
  • 2021-09-30
  • 2022-12-23
相关资源
相似解决方案