codio 3.34nusearch short string list code example
Example: python how to format a string with quotation marks to and array with split(" ")
def format_string_to_array(string):
array = []
array_item = ""
is_quote = False
i = 0
while i < len(string):
if string[i:i+1] == " ":
if is_quote == True:
array_item += string[i]
elif is_quote == False:
if not array_item == "":
array.append(array_item)
array_item = ""
elif string[i:i+1] == "\"" or string[i:i+1] == "\'":
if is_quote == False:
is_quote = True
else:
array.append(array_item)
array_item = ""
is_quote = False
else:
array_item += string[i]
i += 1
array.append(array_item)
return array
a = "Hello \'single array item with spaces\' World!"
print(format_string_to_array(a))