Home > Article > Web Front-end > JS remove punctuation and last character at end of string instance
This article mainly introduces the JS method of removing punctuation marks at the end of a string and deleting the last character. Friends who need it can refer to it
Requirements: Remove the end of a js string Punctuation marks
Original string:
Hello World!
Target string:
Hello World
Method 1:
stringObject.slice(start,end)
start: to be extracted The starting index of the fragment. 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 three:
stringObject.substring(start,stop)
Unlike the slice() and substr() methods, substring() does not accept negative arguments
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 look at several JS methods to delete the last character of a string
String :string s = "1,2,3,4,5,"
1. The most commonly used one is Substring
JS to delete the last character of the string. Several methods - li_crane - The road ahead s=s.Substring(0,s.Length-1)
2. Use RTrim, which is only used to delete the last space. , and I didn’t look at other usages carefully, only to find that I can trim out some characters directly
JS Several ways to delete the last character of a string - 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. This thing is similar to RTrim. The difference What is passed is 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 Delete Several methods of deleting 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 ways to delete 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 that has a slight benefit in improving performance....
string.TrimEnd().Remove(string.Length - 2, 1) string.Remove()
Summary
The above is the detailed content of JS remove punctuation and last character at end of string instance. For more information, please follow other related articles on the PHP Chinese website!