Skip to content

Commit 9e5c669

Browse files
committed
Fix split function to handle trailing delimiters correctly
1 parent 0bcdfbd commit 9e5c669

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

strings/split.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def split(string: str, separator: str = " ") -> list:
1414
1515
>>> split("12:43:39",separator = ":")
1616
['12', '43', '39']
17+
18+
>>> split(";abbb;;c;", separator=';')
19+
['', 'abbb', '', 'c', '']
1720
"""
1821

1922
split_words = []
@@ -24,11 +27,14 @@ def split(string: str, separator: str = " ") -> list:
2427
split_words.append(string[last_index:index])
2528
last_index = index + 1
2629
elif index + 1 == len(string):
27-
split_words.append(string[last_index : index + 1])
30+
split_words.append(string[last_index:index + 1])
31+
32+
if string and string[-1] == separator:
33+
split_words.append('')
34+
2835
return split_words
2936

3037

3138
if __name__ == "__main__":
3239
from doctest import testmod
33-
34-
testmod()
40+
testmod()

0 commit comments

Comments
 (0)