Expanded several common methods. For more information, see
The Code is 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? "]": "}");
}
};