Implementation of TrimStart, TrimEnd, and Trim on javascript in C #

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.