JS string Function Extension code. You can refer to the prototype usage method to expand your own string processing functions.
The Code is as follows:
/*************************************** *************
* CreateBy: joe zhou
* CreateDate: 2011-9-4
* Description: String Auxiliary Function
**************************************** ************/
// String. prototype = {
// Caption: function (){
//},
// LeftPad: function (padChar, width ){
// If (this. length> = width ){
// Return this;
//}
//}
//};
String. prototype. padLeft = function (padChar, width ){
Var ret = this;
While (ret. length <width ){
If (ret. length + padChar. length <width ){
Ret = padChar + ret;
}
Else {
Ret = padChar. substring (0, width-ret.length) + ret;
}
}
Return ret;
};
String. prototype. padRight = function (padChar, width ){
Var ret = this;
While (ret. length <width ){
If (ret. length + padChar. length <width ){
Ret + = padChar;
}
Else {
Ret + = padChar. substring (0, width-ret. length );
}
}
Return ret;
};
String. prototype. trim = function (){
Return this. replace (/^ \ s +/, ''). replace (/\ s + $ /,'');
};
String. prototype. trimLeft = function (){
Return this. replace (/^ \ s + /,'');
};
String. prototype. trimRight = function (){
Return this. replace (/\ s + $ /,'');
};
String. prototype. caption = function (){
If (this ){
Return this. charAt (0). toUpperCase () + this. substr (1 );
}
Return this;
};
String. prototype. reverse = function (){
Var ret = '';
For (var I = this. length-1; I> = 0; I --){
Ret + = this. charAt (I );
}
Return ret;
};
String. prototype. startWith = function (compareValue, ignoreCase ){
If (ignoreCase ){
Return this. toLowerCase (). indexOf (compareValue. toLowerCase () = 0;
}
Return this. indexOf (compareValue) = 0
};
String. prototype. endWith = function (compareValue, ignoreCase ){
If (ignoreCase ){
Return this. toLowerCase (). lastIndexOf (compareValue. toLowerCase () = this. length-compareValue. length;
}
Return this. lastIndexOf (compareValue) = this. length-compareValue. length;
};