If I have a string like
10,000kg crane,21
how should I strip all commas but the last to get
10000kg crane,21
I’m thinking this is a regular expression problem.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Another approach, which may perform much faster than a RegEx solution:
The gist is that it will replace all occurrences of “,” with an empty string between the first character and the index of the last “,”.
I did some benchmarks running this and the proposed RegEx solution 1,000,000 times each; on my laptop, without compiling the RegEx, this solution is about seven (7) times faster. If you do compile the RegEx, it’s still about twice as fast.