Created
February 14, 2018 20:50
-
-
Save ajay2611/c3378bc78dda6af54a1cfd1960184140 to your computer and use it in GitHub Desktop.
Python's split function implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split(string, delimiter): | |
""" | |
Desc: Python's split function implementation | |
:param string: a string | |
:return: a list after breaking string on delimiter match | |
""" | |
result_list = [] | |
if not delimiter: | |
raise ValueError("Empty Separator") | |
if not string: | |
return [string] | |
start = 0 | |
for index, char in enumerate(string): | |
if char == delimiter: | |
result_list.append(string[start:index]) | |
start = index + 1 | |
if start == 0: | |
return [string] | |
result_list.append(string[start:index + 1]) | |
return result_list | |
if __name__ == '__main__': | |
print(split("abc def xyz", " ")) | |
print(split("abc", " ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
won't work if delimiter is more than one character