[英]Split a string by new line without using inbuilt split function
I want to make my own function that splits a string with \\n
(new line).我想制作自己的函数,用
\\n
(新行)分割字符串。 I do not want to use any inbuilt function.我不想使用任何内置功能。 How can this be done?
如何才能做到这一点?
try this:尝试这个:
def split_string(string, delimiter):
output = []
while delimiter in string:
index = string.index(delimiter)
output.append(string[0:index])
string = string[index+1:]
output.append(string)
return output
str = """This is
a string
of words
delimited
by slash n
ok"""
split_string(str, "\n")
op:操作:
['This is ', 'a string', 'of words', 'delimited', 'by slash n', 'ok']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.