I got String from the database which have multiple commas (,) . I want to remove the last comma but I can’t really find a simple way of doing it.
What I have: kushalhs, mayurvm, narendrabz,
What I want: kushalhs, mayurvm, narendrabz
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.
To remove the
", "part which is immediately followed by end of string, you can do:This handles the empty list (empty string) gracefully, as opposed to
lastIndexOf/substringsolutions which requires special treatment of such case.Example code:
NOTE: Since there has been some comments and suggested edits about the
", $"part: The expression should match the trailing part that you want to remove."a,b,c,", use",$"."a, b, c, ", use", $"."a , b , c , ", use" , $".I think you get the point.