Expanded a few commonly used methods, the need for friends can refer to the following
copy code code as follows:
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? "]": "}");
}
};