I would like a regular expression or otherwise some method to remove the last character in a string if and only if that character is '/'. How can I do it?
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove. Remove last character of a string using sb. deleteCharAt(str.
Use the . rstrip() method to remove whitespace and characters only from the end of a string.
string = string.replace(/\/$/, "");
$
marks the end of a string. \/
is a RegExp-escaped /
. Combining both = Replace the /
at the end of a line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With