JS use regular to remove the last comma _ regular expression of the string

Source: Internet
Author: User
Tags rtrim

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.

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.