Several ways JavaScript removes the last character of a string

Source: Internet
Author: User
Tags rtrim

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

Goal: Delete the last ","


Method:

1, the most used is substring, this is what I have been using

S=s.substring (0,s.length-1)


2, with RTrim, this I originally only know to remove the last space, also did not carefully read the other usage, only found that can be directly trim off some characters

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


3, with trimend, this thing and RTrim almost, the difference is that this pass is a character array, and RTrim can be any valid string

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 that has a slight benefit in improving performance ....

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

String. Remove ()

This article is from the "Silent http://www.51pro.net" blog, please be sure to keep this source http://ruiwen.blog.51cto.com/8852761/1706187

Several ways JavaScript removes the last character of a string

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.