Home >Web Front-end >JS Tutorial >javascript deletes the last character of a string_javascript tips

javascript deletes the last character of a string_javascript tips

WBOY
WBOYOriginal
2016-05-16 17:03:451120browse

I searched a lot online and summarized several methods for your reference:

1. The most used one is Substring, which is what I always use.

Copy the code The code is as follows:

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

2. Use RTrim. I originally only knew how to delete the last spaces, and I didn’t look at other usages carefully. Then I found that I can directly trim out some characters

Copy code The code is as follows:

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

3. Use TrimEnd, which is similar to RTrim. The difference is that this passes a character array, and RTrim can be any valid string

Copy code The code is as follows:

s=s.TrimEnd(',')
//If you want to delete "5,", you need to write like this
char[]MyChar={'5′,','};
s=s.TrimEnd(MyChar);
//s=”1,2,3,4″
Statement:
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