Skip to content

Commit 927c7d3

Browse files
MD-Mushfiqur123Codebuff Contributor
andauthored
fix(split): support multi-character separators (#14685)
Signed-off-by: god032396-del <god032396@gmail.com> Co-authored-by: Codebuff Contributor <contributor@codebuff.com>
1 parent 5ce6733 commit 927c7d3

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

strings/split.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,31 @@ def split(string: str, separator: str = " ") -> list:
1717
1818
>>> split(";abbb;;c;", separator=';')
1919
['', 'abbb', '', 'c', '']
20+
21+
>>> split("a--b--c", separator="--")
22+
['a', 'b', 'c']
23+
24+
>>> split("apple##banana##cherry", separator="##")
25+
['apple', 'banana', 'cherry']
2026
"""
2127

2228
split_words = []
29+
separator_length = len(separator)
30+
31+
if separator_length == 0:
32+
return [string]
2333

2434
last_index = 0
25-
for index, char in enumerate(string):
26-
if char == separator:
35+
index = 0
36+
while index < len(string):
37+
if string[index : index + separator_length] == separator:
2738
split_words.append(string[last_index:index])
28-
last_index = index + 1
29-
if index + 1 == len(string):
30-
split_words.append(string[last_index : index + 1])
39+
last_index = index + separator_length
40+
index += separator_length
41+
else:
42+
index += 1
43+
44+
split_words.append(string[last_index:])
3145
return split_words
3246

3347

0 commit comments

Comments
 (0)