Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 18:11:43
Original
1137 people have browsed it

So, I wrote it myself!! I saw many people using regular expressions, but I didn’t know how to do it, so I used the most primitive method to achieve it! Post the code! I hope it will be helpful to everyone!!!

Copy code The 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);
};

Everyone should know how to use it!!! I won’t go into details here!!! If you have any questions, please point them out! Thank you!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!