string s = "1,2,3,4,5,"
Target: Delete the last ","
method:
The most commonly used substring is also what I have always used
s=s.substring(0,s.length-1)
I have been encountering this kind of thing recently, such as a string "[lightinthebox]", but I just need lightinthebox, just don't "[]". How to remove it in a fast and effective way?
stringObject.substring(start,stop) //The strings that intercept the start and end.
stringObject.substr(start,length) //The intercept is the beginning and the string length.
Taking these into account, the connection with the method.
stringObject.substr(1).substring(-1,0) //It's feasible
Let’s talk about the intercepting time here. Now we all want to change the single digit into two digits. For example, 9 is displayed as 09 for easier format alignment.
Many places determine whether this number is less than 10 to determine whether it is added 0.
If we use strings, we don’t have to judge. Add one and intercept the last two digits. 01, 010, 011 becomes 01 10 011
I won't say anything specific, so that no one will laugh at me
Later, because the Wulin Network background needed to add some small functions, I specially thanked a function to determine whether the last character is first and then replace it.
<SCRIPT type="text/javascript">function delfh(str){str=str.replace(",,",",");if(str.substring(str.length-1,str.length)== ","){str2=str.substring(0,str.length-1);delfh(str2);}else{str2=str;}return str2;}var s2="1,,,2,,,, 3,,,,4,54,454,,,,,,,,,,,,,,,,,,";var s="415929,415930,415931,415932,415933,415934,415935,415936,415937,415938, 415939,415940,415941,415942,415943,415944,415945,415946,415947,415948,1,2,3";alert(delfh(s2));</script>
No problem after testing.