Strings: string s = "1,2,3,4,5,"
Goal: Delete the last ","
Method:
1, use the most is substring, this is also I have been used, must pay attention to the case, cloud Habitat Community Small series has been tested.
Copy Code code as follows:
var s = "1,2,3,4,5,"
S=s.substring (0,s.length-1)
alert (s);
2, the use of regular expressions to achieve
Copy Code code as follows:
var str= "A,b,c,d,"
var reg=/,$/gi;
Str=str.replace (Reg, "");
alert (str);
3, with prototype expansion
Copy Code code as follows:
<script type= "Text/javascript" >
Deletes the character at the specified index location, and the index is invalid without deleting any characters
String.prototype.deletecharat=function (Sindex) {
if (sindex<0 | | sindex>=this.length) {
return this.valueof ();
}else if (sindex==0) {
Return this.substring (1,this.length);
}else if (sindex==this.length-1) {
Return this.substring (0,this.length-1);
}else{
Return this.substring (0,sindex) +this.substring (sindex+1);
}
}
The above function must be placed above, otherwise it will not work
var s = "1,2,3,4,5,";
var index = s.tostring (). LastIndexOf (', ');
var s=s.deletecharat (index);
alert (s);
</script>
4, with RTrim, this I used to only know to delete the last space, nor have carefully looked at other uses, only to find that can be directly trim off some characters
Copy Code code as follows:
S=s.tostring (). RTrim (', ')
5, with trimend, this thing and RTrim is similar, the difference is this pass is a character array, and RTrim can be any valid string
Copy Code code as follows:
S=s.trimend (', ')
If you want to delete "5," you need to write this
char[]mychar={' 5 ', ', '};
S=s.trimend (MyChar);
S= "1,2,3,4"
Similar functions:
Trimstart,ltrim, etc.
There is also a trimtosize to improve performance has a weak advantage ....
Copy Code code as follows:
String. TrimEnd (). Remove (String. Length-2, 1)
String. Remove ()
Note: The first three kinds of cloud habitat after the small series of sorting and testing, can be used normally, recommended that a second method, from the fourth after no test, are through a custom function to achieve, we can expand their own, pay special attention to the case.