Home > Web Front-end > JS Tutorial > Summary of Javascript extending String methods_javascript skills

Summary of Javascript extending String methods_javascript skills

WBOY
Release: 2016-05-16 17:29:47
Original
980 people have browsed it
复制代码 代码如下:

String.prototype.EndWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false;
    if (this.substring(this.length - str.length) == str)
        return true;
    else
        return false;
    return true;
}
String.prototype.StartWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false;
    if (this.substr(0, str.length) == str)
        return true;
    else
        return false;
    return true;
}
String.prototype.Trim = function () {
    return this.replace(/(^s*)|(s*$)/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/(^s*)/g, "");
}
String.prototype.rtrim = function () {
    return this.replace(/(s*$)/g, "");
}
String.format = function (str) {
    var i = 1, args = arguments;
    var str = args[0];
    var re = /{(d )}/g;
    return str.replace(re, function () { return args[i ] });
};
var Json2string = 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 && obj.constructor == Array);
                for (n in obj) {
                    v = obj[n]; t = typeof (v);
                    if (t == "string") v = "'" v "'";
                    else if (t == "object" && v !== null)
                        v = Json2string(v);
                    json.push((arr ? "" : "'" n "':") String(v));
                }
                return (arr ? "[" : "{") String(json) (arr ? "]" : "}");
            }
        }; 
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template