Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove the comma at the end of a string with JS rules

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use JS to regularly remove the last comma of a string". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

String: string s = "1, 2, 3, 4, 5,"

Goal: delete the last ","

Methods:

1, the most used is substring, this is also what I have been using, be sure to pay attention to case, the editor has been tested.

The copy code is as follows:

Var s = "1, 2, 3, 4, 5,"

S=s.substring (0dint s.futhmur1)

Alert (s)

2. Implement with regular expression

The copy code is as follows:

Var str= "a recorder bpm cpene d,"

Var reg=/,$/gi

Str=str.replace (reg, "")

Alert (str)

3. Expand with prototype

The copy code is as follows:

/ / Delete the characters at the specified index position. Invalid index will not delete any characters.

String.prototype.deleteCharAt=function (sIndex) {

If (sIndex=this.length) {

Return this.valueOf ()

} else if (sIndex==0) {

Return this.substring (1this.length)

} else if (sIndex==this.length-1) {

Return this.substring (0Magnum this.futhmur1)

} else {

Return this.substring (0Magazine Index) + this.substring (sIndex+1)

}

}

/ / the above function must be put on top, otherwise it won't work.

Var s = "1, 2, 3, 4, 5,"

Var index = s.toString () .lastIndexOf (',')

Var s=s.deleteCharAt (index)

Alert (s)

4. Using RTrim, which I only knew to delete the last space, and had not carefully looked at other uses, found that it was possible to trim some characters directly

The copy code is as follows:

S=s.ToString (). RTrim (',')

5. With TrimEnd, this thing is similar to RTrim, except that it passes an array of characters, while RTrim can be any valid string

The copy code is as follows:

S=s.TrimEnd (',')

/ / if you want to delete "5," you need to write this

Char [] MyChar= {'5pm.

S=s.TrimEnd (MyChar)

/ / s = "1, 2, 3, 4"

Similar functions:

TrimStart,LTrim et al.

There is also a TrimToSize that has a slight benefit in improving performance.

The copy code is as follows:

String.TrimEnd () .Remove (string.Length-2,1)

String.Remove ()

Note: the first three can be used normally after finishing and testing by editors. The first and second methods are recommended. There are no tests since the fourth. They are all realized through custom functions. You can extend them by yourself, paying special attention to the case.

This is the end of "how to remove the last comma of a string with JS rules". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report