string s = "1,2,3,4,5,"
Target: Delete the last ","
method:
1. The most commonly used substring is substring, which I have always used. You must pay attention to upper and lower case. My editor has tested it.
Copy the codeThe code is as follows:
var s = "1,2,3,4,5,"
s=(0,-1)
alert(s);
2. Use regular expressions to implement
Copy the codeThe code is as follows:
var str="a,b,c,d,"
var reg=/,$/gi;
str=(reg,"");
alert(str);
3. Extend with prototype
Copy the codeThe code is as follows:
<script type="text/javascript">
//Delete characters at the specified index position. If the index is invalid, no characters will be deleted.
=function(sIndex){
if(sIndex<0 || sIndex>=){
return ();
}else if(sIndex==0){
return (1,);
}else if(sIndex==-1){
return (0,-1);
}else{
return (0,sIndex)+(sIndex+1);
}
}
//The above function must be placed on it, otherwise it will not work
var s = "1,2,3,4,5,";
var index = ().lastIndexOf(',');
var s=(index);
alert(s);
</script>
4. Use RTrim, I used to only use it to delete the last space, and I haven't carefully looked at other usages, so I found that I could directly trim some characters
Copy the codeThe code is as follows:
s=().RTrim(',')
5. With TrimEnd, this thing is similar to RTrim. The difference is that this passes a character array, while RTrim can be any valid string
Copy the codeThe code is as follows:
s=(',')
//If you want to delete "5," you need to write this
char[]MyChar={'5',','};
s=(MyChar);
//s="1,2,3,4"
Similar functions:
TrimStart, LTrim, etc.
There is also a TrimToSize that has a slight benefit to improving performance...
Copy the codeThe code is as follows:
().Remove( - 2, 1)
()
Note: The first three types have been sorted and tested by my editor and can be used normally. The first and second methods are recommended. There is no test since the fourth type. They are all implemented through custom functions. You can expand them by yourself, paying special attention to lower case.