JS removes the punctuation marks at the end of the string and the method for deleting the last character. js punctuation marks

Source: Internet
Author: User
Tags rtrim

JS removes the punctuation marks at the end of the string and the method for deleting the last character. js punctuation marks

Requirement: remove the punctuation marks at the end of the js string.

Original string:

Hello World!

Target string:

Hello World

Method 1:

stringObject.slice(start,end)

Start: start subscript of the part to be extracted. If it is a negative number, this parameter specifies the position starting from the end of the string. That is,-1 refers to the last character of the string.

End: subscript of the end of the clip to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If this parameter is a negative number, it specifies the position starting from the end of the string.

Var str = 'Hello World! '; Document. write (str. slice (0, str. length-1); // output Hello World

Method 2:

stringObject.substr(start,length)

Start: required. The starting subscript of the substring to be extracted. It must be a numerical value. If it is a negative number, this parameter declares the position starting from the end of the string. That is to say,-1 refers to the last character in the string,-2 refers to the second to last character, and so on.

Length: Optional. The number of characters in the substring. It must be a numerical value. If this parameter is omitted, the string from the start position of the stringObject to the end is returned.

Var str = 'Hello World! '; Document. write (str. substr (0, str. length-1); // output Hello World

Method 3:

stringObject.substring(start,stop)

Different from slice () and substr (), substring () does not accept negative parameters.

Start: required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject

Stop: Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject.

If this parameter is omitted, the returned substring ends at the end of the string.

Var str = 'Hello World! '; Document. write (str. substr (0, str. length); // output Hello World

The following describes how to delete the last character of a string in JS.

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

1. Substring is the most used

JS methods for deleting The last character of a string-li_crane-The road to The future (The road ahead) s = s. Substring (0, s. Length-1)

2. When RTrim is used, it was used to delete the last space and did not carefully read other usage, only to find that trim can directly drop some characters.

JS methods for deleting The last character of a string-li_crane-The road to The future (The road ahead) s = s. ToString (). RTrim (',')

Extended Space Deletion

Function trim (str) {// Delete the leading and trailing spaces return str. replace (/(^ \ s *) | (\ s * $)/g, "");} function ltrim (str) {// Delete the left space return str. replace (/(^ \ s *)/g, "");} function rtrim (str) {// Delete the right space return str. replace (/(\ s * $)/g ,"");}

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

JS methods for deleting The last character of a string-li_crane-The road to The future (The road ahead) s = s. TrimEnd (',')
JS methods for deleting The last character of a string-li_crane-The road to The future (The road ahead) // This is required if you want to delete "5 ,"
JS methods for deleting The last character of a string-li_crane-The road to The future (The road ahead) char [] MyChar = {'5 ',','};
JS methods for deleting The last character of a string-li_crane-The road to The future (The road ahead) s = s. TrimEnd (MyChar );
JS methods for deleting The last character of a string-li_crane-The road to The future (The road ahead) // s = "1, 2, 3, 4"

Similar functions:

TrimStart, LTrim, etc.

There is also a TrimToSize that has minor benefits in improving performance ....

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

Summary

The above section describes how to remove the punctuation marks at the end of the string and delete the last character in Javascript. I hope this will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

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.