Home > Web Front-end > JS Tutorial > body text

JS method to remove punctuation marks at the end of string

小云云
Release: 2018-01-03 16:29:11
Original
2766 people have browsed it

This article mainly introduces the JS method of removing punctuation marks at the end of a string and deleting the last character. Friends in need can refer to it. I hope it can help everyone.

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)
Copy after login

start: The starting index of the fragment to be extracted. If it is a negative number, this parameter specifies the position starting from the end of the string. In other words, -1 refers to the last character of the string

end: the subscript of the end of the fragment 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, then it specifies the position starting from the end of the string

var str = 'Hello World!';
document.write(str.slice(0,str.length-1)); //输出 Hello World
Copy after login

Method 2:

stringObject.substr(start,length)
Copy after login

start: Required. The starting index of the substring to be extracted. Must be a numeric value. If negative, this parameter declares the position from the end of the string. That is, -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. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of stringObject is returned.

var str = 'Hello World!';
document.write(str.substr(0,str.length-1)); //输出 Hello World
Copy after login

Method 3:

stringObject.substring(start,stop)
Copy after login

Unlike the slice() and substr() methods, 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 stringObject

stop : Optional. A nonnegative integer that is one position in the stringObject that is one more than the last character of the substring to be extracted.

If this parameter is omitted, the returned substring will go to the end of the string.

var str = 'Hello World!';
document.write(str.substr(0,str.length)); //输出 Hello World
Copy after login

Let’s take a look at several ways to delete the last character of a string in JS

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

1. The most commonly used one is Substring

JS Several ways to delete the last character of a string - li_crane - The road ahead (The road ahead)s=s.Substring(0,s.Length-1 )

2. Use RTrim. It was originally only used to delete the last space. I didn’t look at other usages carefully, only to find that I can directly trim out some characters.

JS Delete Characters Several ways to string the last character - li_crane - The road ahead (The road ahead)s=s.ToString().RTrim(',')

Expand and delete spaces

function trim(str){ //删除左右两端的空格
  return str.replace(/(^\s*)|(\s*$)/g, "");
 }
 function ltrim(str){ //删除左边的空格
 return str.replace(/(^\s*)/g,"");
 }
 function rtrim(str){ //删除右边的空格
  return str.replace(/(\s*$)/g,"");
 }
Copy after login

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

JS Several ways to delete the last character of a string - li_crane - The road ahead (The road ahead)s=s.TrimEnd(',')
JS Several ways to delete the last character of a string - li_crane - The road ahead (The road ahead)//If you want to delete "5,", you need to write like this
JS Several ways to delete the last character of a string - li_crane - The road ahead (The road ahead)char[]MyChar={'5',','};
JS Several methods of deleting the last character of a string - li_crane - The road ahead (The road ahead)s=s.TrimEnd(MyChar);
JS Several methods of deleting the last character of a string - li_crane - The road ahead (The road ahead)//s="1,2,3,4"

Similar functions:

TrimStart, LTrim, etc.

There is also a TrimToSize It has slight benefits in improving performance....

string.TrimEnd().Remove(string.Length - 2, 1)
string.Remove()
Copy after login

Related recommendations:

detailed explanation of vue syntax for splicing strings

phpHow to realize the mutual exchange of substring positions

js Example of extracting the length of a certain () special string_javascript skills

The above is the detailed content of JS method to remove punctuation marks at the end of string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!