javascript中的String.Format与vb.net中的一样

javascript中的String.Format与vb.net中的一样,javascript,string,format,Javascript,String,Format,javascript中是否有类似于vb.net的String.Format代码 val=String.Format("{0:n" & ratedis & "}", to_arr(0)) 这很简单。别担心。把格式换成格式就行了。并将以下脚本文件添加到项目中 val= String.format("{0:n" + ratedis + "}", to_arr[0]); String.js // JScript File //String.js-由Sky Sanders于20

javascript中是否有类似于vb.net的String.Format代码

 val=String.Format("{0:n" & ratedis & "}", to_arr(0))

这很简单。别担心。把格式换成格式就行了。并将以下脚本文件添加到项目中

 val= String.format("{0:n" + ratedis + "}", to_arr[0]);
String.js

 // JScript File
//String.js-由Sky Sanders于2010年3月28日从MicrosoftAjax.js中解放出来

/* 版权所有(C)2009,CODULTEX基金会 版权所有

Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

*   Redistributions of source code must retain the above copyright notice, this list of conditions 
    and the following disclaimer.

*   Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
    and the following disclaimer in the documentation and/or other materials provided with the distribution.

*   Neither the name of CodePlex Foundation nor the names of its contributors may be used to endorse or 
    promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</textarea>
允许以源代码和二进制格式重新分发和使用,无论是否修改
但须符合下列条件:
*源代码的重新分发必须保留上述版权声明、此条件列表
以及以下免责声明。
*以二进制形式重新分发时,必须复制上述版权声明、此条件列表
以及随分发提供的文档和/或其他材料中的以下免责声明。
*无论是CODULTEX基金会的名称,还是其贡献者的姓名,都不可用于背书或
未经事先书面许可,促销源自本软件的产品。
本软件由版权所有者和贡献者按原样以及任何明示或暗示的方式提供
保证,包括但不限于适销性和适用性的默示保证
不承认有特定目的。在任何情况下,版权所有人或贡献者概不负责
任何直接、间接、附带、特殊、惩戒性或后果性损害(包括但不限于
仅限于,替代商品或服务的采购;使用、数据或利润的损失;或业务损失
中断)无论是何种原因造成的,且基于任何责任理论,无论是在合同中,还是在严格责任中,
或因使用本软件而产生的侵权行为(包括疏忽或其他),甚至
如果告知可能发生此类损坏。
*/

(功能(窗口){
$type=字符串;
$type.\uuuTypeName='String';
$type.\uu class=true;
$prototype=$type.prototype;
$prototype.endsWith=函数字符串$endsWith(后缀){
///确定此实例的结尾是否与指定的字符串匹配。
///要比较的字符串。
///如果后缀匹配此实例的结尾,则为true;否则为false。
返回(this.substr(this.length-suffix.length)==后缀);
}
$prototype.startsWith=函数字符串$startsWith(前缀){
///确定此实例的开头是否与指定的字符串匹配。
///要比较的字符串。
///如果前缀匹配此字符串的开头,则为true;否则为false。
返回(this.substr(0,prefix.length)==前缀);
}
$prototype.trim=函数字符串$trim(){
///从当前字符串对象中删除所有前导和尾随空格字符。
///从当前字符串对象的开头和结尾删除所有空白字符后保留的字符串。
返回此。替换(/^\s+|\s+$/g,');
}
$prototype.trimEnd=函数字符串$trimEnd(){
///从当前字符串对象中删除所有尾随空格。
///从当前字符串对象的末尾删除所有空白字符后保留的字符串。
返回此。替换(/\s+$/,“”);
}
$prototype.trimStart=函数字符串$trimStart(){
///从当前字符串对象中删除所有前导空格。
///从当前字符串对象的开头删除所有空白字符后保留的字符串。
返回此。替换(/^\s+/,“”);
}
$type.format=函数字符串$format(格式,args){
///将指定字符串中的格式项替换为相应对象实例值的文本等效项。不变区域性将用于格式化日期和数字。
///格式字符串。
///要格式化的对象。
///格式的副本,其中格式项已被对象参数的相应实例的等效字符串替换。
返回字符串.tofu-formattedString(false,参数);
}
$type.\u-toFormattedString=函数字符串$\u-toFormattedString(useLocale,args){
var结果=“”;
var format=args[0];
对于(变量i=0;;){
//查找下一个打开或关闭的大括号
var open=format.indexOf('{',i);
var close=format.indexOf('}',i);
如果((打开<0)和&(关闭<0)){
//未找到:复制字符串的结尾并断开
结果+=格式切片(i);
打破
}
如果((关闭>0)和((关闭<打开)| |(打开<0))){
if(format.charAt(close+1)!=='}'){
抛出新错误('format stringFormatBraceMismatch');
}
结果+=format.slice(i,close+1);
i=关闭+2;
持续
}
//将字符串复制到大括号之前
结果+=format.slice(i,打开);
i=打开+1;
//检查是否有双大括号(显示为一个大括号且不是参数)
if(format.charAt(i)=='{'){
结果+='{';
i++;
持续
}
if(close<0)抛出新错误('format stringFormatBraceMismatch');
//找到右括号
//获取大括号之间的字符串,并将其围绕“:”(如果有)拆分
var brace=format.substring(i,close);
var colonIndex=brace.indexOf(':');
var argNumber=parseInt((colonIndex<0)?大括号:brace.substring(0,colonIndex),10)+1;
if(isNaN(argNumber))抛出新错误('format stringFormatInvalid');
var argFormat=(colonIndex<0)?“”:大括号.substring(colonIndex+1);
var arg=args[argNumber];
if(typeof(arg)==“未定义”|| arg==null){
arg='';
}
//如果它有toFormattedString方法,则调用它。否则,调用toString()
if(参数至格式字符串){
结果+=arg.toFormattedString(argFormat);
}
else if(使用locale&&arg.l
(function(window) {

$type = String;
$type.__typeName = 'String';
$type.__class = true;

$prototype = $type.prototype;
$prototype.endsWith = function String$endsWith(suffix) {
    /// <summary>Determines whether the end of this instance matches the specified string.</summary>
    /// <param name="suffix" type="String">A string to compare to.</param>
    /// <returns type="Boolean">true if suffix matches the end of this instance; otherwise, false.</returns>
    return (this.substr(this.length - suffix.length) === suffix);
}

$prototype.startsWith = function String$startsWith(prefix) {
    /// <summary >Determines whether the beginning of this instance matches the specified string.</summary>
    /// <param name="prefix" type="String">The String to compare.</param>
    /// <returns type="Boolean">true if prefix matches the beginning of this string; otherwise, false.</returns>
    return (this.substr(0, prefix.length) === prefix);
}

$prototype.trim = function String$trim() {
    /// <summary >Removes all leading and trailing white-space characters from the current String object.</summary>
    /// <returns type="String">The string that remains after all white-space characters are removed from the start and end of the current String object.</returns>
    return this.replace(/^\s+|\s+$/g, '');
}

$prototype.trimEnd = function String$trimEnd() {
    /// <summary >Removes all trailing white spaces from the current String object.</summary>
    /// <returns type="String">The string that remains after all white-space characters are removed from the end of the current String object.</returns>
    return this.replace(/\s+$/, '');
}

$prototype.trimStart = function String$trimStart() {
    /// <summary >Removes all leading white spaces from the current String object.</summary>
    /// <returns type="String">The string that remains after all white-space characters are removed from the start of the current String object.</returns>
    return this.replace(/^\s+/, '');
}

$type.format = function String$format(format, args) {
    /// <summary>Replaces the format items in a specified String with the text equivalents of the values of   corresponding object instances. The invariant culture will be used to format dates and numbers.</summary>
    /// <param name="format" type="String">A format string.</param>
    /// <param name="args" parameterArray="true" mayBeNull="true">The objects to format.</param>
    /// <returns type="String">A copy of format in which the format items have been replaced by the   string equivalent of the corresponding instances of object arguments.</returns>
    return String._toFormattedString(false, arguments);
}

$type._toFormattedString = function String$_toFormattedString(useLocale, args) {
    var result = '';
    var format = args[0];

    for (var i = 0; ; ) {
        // Find the next opening or closing brace
        var open = format.indexOf('{', i);
        var close = format.indexOf('}', i);
        if ((open < 0) && (close < 0)) {
            // Not found: copy the end of the string and break
            result += format.slice(i);
            break;
        }
        if ((close > 0) && ((close < open) || (open < 0))) {

            if (format.charAt(close + 1) !== '}') {
                throw new Error('format stringFormatBraceMismatch');
            }

            result += format.slice(i, close + 1);
            i = close + 2;
            continue;
        }

        // Copy the string before the brace
        result += format.slice(i, open);
        i = open + 1;

        // Check for double braces (which display as one and are not arguments)
        if (format.charAt(i) === '{') {
            result += '{';
            i++;
            continue;
        }

        if (close < 0) throw new Error('format stringFormatBraceMismatch');


        // Find the closing brace

        // Get the string between the braces, and split it around the ':' (if any)
        var brace = format.substring(i, close);
        var colonIndex = brace.indexOf(':');
        var argNumber = parseInt((colonIndex < 0) ? brace : brace.substring(0, colonIndex), 10) + 1;

        if (isNaN(argNumber)) throw new Error('format stringFormatInvalid');

        var argFormat = (colonIndex < 0) ? '' : brace.substring(colonIndex + 1);

        var arg = args[argNumber];
        if (typeof (arg) === "undefined" || arg === null) {
            arg = '';
        }

        // If it has a toFormattedString method, call it.  Otherwise, call toString()
        if (arg.toFormattedString) {
            result += arg.toFormattedString(argFormat);
        }
        else if (useLocale && arg.localeFormat) {
            result += arg.localeFormat(argFormat);
        }
        else if (arg.format) {
            result += arg.format(argFormat);
        }
        else
            result += arg.toString();

        i = close + 1;
    }

    return result;
}

})(window);