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)
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
Method 2:
stringObject.substr(start,length)
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
Method 3:
stringObject.substring(start,stop)
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
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,""); }
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()
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!