Is there a function to chomp last character in the string if it's some special character? For example, I need to remove backslash if it's there, and do nothing, if not. I know I can do it with regex easily, but wonder if there something like a small built-in function for that.
rstrip('\n') . This will strip all newlines from the end of the string, not just one.
Using replace() Function to Remove Backslash from String in Python. The replace() can be utilized to remove an escape sequence or just a backslash in Python by simply substituting it with space in Python.
Use the deleteCharAt Method to Remove a Character From String in Java. The deleteCharAt() method is a member method of the StringBuilder class that can also be used to remove a character from a string in Java.
Use rstrip
to strip the specified character(s) from the right side of the string.
my_string = my_string.rstrip('\\')
See: http://docs.python.org/library/stdtypes.html#str.rstrip
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