Javascript removes the last character of the string

  • 2020-03-30 01:21:56
  • OfStack

On the Internet to find a lot of, summed up several methods for your reference:

1. The most commonly used is Substring, which is also what I have been using


s=s.Substring(0,s.Length-1)

2, with RTrim, which I only know to delete the last space, also did not carefully look at other usage, only found that can directly trim off some characters


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

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


s=s.TrimEnd( ' ,')
//If you want to delete "5," you need to do this
char[]MyChar={'5 ' ,','};
s=s.TrimEnd(MyChar);
//s= " 1,2,3,4 " 


Related articles: