So, I wrote it myself !! We can see that many people use regular expressions. We don't know. We just use the most earthy method to implement it! Post Code! Hope to help you !!!
Copy codeThe Code is as follows:
String. prototype. trimStart = function (trimStr ){
If (! TrimStr) {return this ;}
Var temp = this;
While (true ){
If (temp. substr (0, trimStr. length )! = TrimStr ){
Break;
}
Temp = temp. substr (trimStr. length );
}
Return temp;
};
String. prototype. trimEnd = function (trimStr ){
If (! TrimStr) {return this ;}
Var temp = this;
While (true ){
If (temp. substr (temp. length-trimStr.length, trimStr. length )! = TrimStr ){
Break;
}
Temp = temp. substr (0, temp. length-trimStr.length );
}
Return temp;
};
String. prototype. trim = function (trimStr ){
Var temp = trimStr;
If (! TrimStr) {temp = "";}
Return this. trimStart (temp). trimEnd (temp );
};
The usage should be clear !!! I won't talk about it here !!! Please specify if you have any questions! Thank you!