熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

Javascript拓展String方法小結

2013-11-15 12:01:30  來源: JSP教程 
拓展了幾個常用的方法需要的朋友可以參考下   復制代碼 代碼如下:

  
StringprototypeEndWith = function (str) {
    if (str == null || str == "" || thislength == || strlength > thislength)
        return false;
    if (thissubstring(thislength strlength) == str)
        return true;
    else
        return false;
    return true;
}
StringprototypeStartWith = function (str) {
    if (str == null || str == "" || thislength == || strlength > thislength)
        return false;
    if (thissubstr( strlength) == str)
        return true;
    else
        return false;
    return true;
}
StringprototypeTrim = function () {
    return thisreplace(/(^s*)|(s*$)/g "");
}
Stringprototypeltrim = function () {
    return thisreplace(/(^s*)/g "");
}
Stringprototypertrim = function () {
    return thisreplace(/(s*$)/g "");
}
Stringformat = function (str) {
    var i = args = arguments;
    var str = args[];
    var re = /{(d+)}/g;
    return strreplace(re function () { return args[i++] });
};
var Jsonstring = function (obj) {
            var t = typeof (obj);
            if (t != "object" || obj === null) {
                // simple data type        
                if (t == "string") obj = "" + obj + "";
                return String(obj);
            }
            else {
                // recurse array or object        
                var n v json = [] arr = (obj && objconstructor == Array);
                for (n in obj) {
                    v = obj[n]; t = typeof (v);
                    if (t == "string") v = "" + v + "";
                    else if (t == "object" && v !== null)
                        v = Jsonstring(v);
                    jsonpush((arr ? "" : "" + n + ":") + String(v));
                }
                return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
            }
        };


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20105.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.