What is fastest way to remove the last character from a string?
I have a string like
a,b,c,d,e,
I would like to remove the last ‘,’ and get the remaining string back:
OUTPUT: a,b,c,d,e
What is the fastest way to do this?
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.
Contrary to the question asked,
rtrim()will remove any number of characters, listed in the second argument, from the end of the string. In case you expect just a single comma, the following code would do:But in my case I had 2 characters, a comma and a space, so I had to change to
and now it would remove all commas and spaces from the end of the string, returning
a, b, c, d, eeither froma, b, c, d, e,,a, b, c, d, e,,,,a, b, c, d, e,ora, b, c, d, e , ,, , ,But in case there could be multiple commas but you need to remove only the last one, then
rtrim()shouldn’t be used at all – see other answers for the solution that directly answers the question.However,
rtrim()could be a good choice if you don’t know whether the extra character could be present or not. Unlikesubstr-based solutions it will returna, b, c, d, efroma, b, c, d, e