Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove last / if it exists as the last character in the string

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?

like image 997
P.Brian.Mackey Avatar asked Sep 29 '11 19:09

P.Brian.Mackey


People also ask

How do I remove a specific character from a string in regex?

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]+", "")

How do you remove the last letter of a string in Java?

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.

How do you remove the last part of a string in Python?

Use the . rstrip() method to remove whitespace and characters only from the end of a string.


1 Answers

string = string.replace(/\/$/, ""); 

$ marks the end of a string. \/ is a RegExp-escaped /. Combining both = Replace the / at the end of a line.

like image 94
Rob W Avatar answered Oct 05 '22 23:10

Rob W