2020-01-18 12:24:33 +00:00
|
|
|
def split(string: str, separator: str = " ") -> list:
|
2020-01-08 13:18:17 +00:00
|
|
|
"""
|
2020-01-18 12:24:33 +00:00
|
|
|
Will split the string up into all the values separated by the separator (defaults to spaces)
|
2020-01-08 13:18:17 +00:00
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
>>> split("apple#banana#cherry#orange",separator='#')
|
2020-01-08 13:18:17 +00:00
|
|
|
['apple', 'banana', 'cherry', 'orange']
|
|
|
|
|
|
|
|
>>> split("Hello there")
|
|
|
|
['Hello', 'there']
|
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
>>> split("11/22/63",separator = '/')
|
2020-01-08 13:18:17 +00:00
|
|
|
['11', '22', '63']
|
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
>>> split("12:43:39",separator = ":")
|
2020-01-08 13:18:17 +00:00
|
|
|
['12', '43', '39']
|
|
|
|
"""
|
|
|
|
|
|
|
|
split_words = []
|
|
|
|
|
|
|
|
last_index = 0
|
|
|
|
for index, char in enumerate(string):
|
2020-01-18 12:24:33 +00:00
|
|
|
if char == separator:
|
2020-01-08 13:18:17 +00:00
|
|
|
split_words.append(string[last_index:index])
|
|
|
|
last_index = index + 1
|
|
|
|
elif index + 1 == len(string):
|
|
|
|
split_words.append(string[last_index : index + 1])
|
|
|
|
return split_words
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod()
|