Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ Examples:

# Remove whitespace and punctuation from end of string
assert remove_end_characters('lalala,.,"') == "lalala"
assert remove_end_characters("") == ""
assert remove_end_characters('lalala, .\t/,"', f"{punctuation}{whitespace}" == "lalala"

# Remove list of items from end of string, stripping any whitespace
Expand Down
2 changes: 1 addition & 1 deletion src/hdx/utilities/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def remove_end_characters(string: str, characters_to_remove: str = punctuation)
Returns:
String with any characters at end of string that are in characters_to_remove removed
"""
while string[-1] in characters_to_remove:
while string and string[-1] in characters_to_remove:
string = string[:-1]
return string

Expand Down
2 changes: 2 additions & 0 deletions tests/hdx/utilities/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def test_normalise(self):

def test_remove_end_characters(self):
assert remove_end_characters('lalala,.,"') == "lalala"
assert remove_end_characters("") == ""
assert remove_end_characters("", ".") == ""
assert (
remove_end_characters('lalala, .\t/,"', f"{punctuation}{whitespace}")
== "lalala"
Expand Down
Loading