Jump to content

Tokenize a string: Difference between revisions

m (→‎{{header|REXX}}: added needed 1st statement for REXX programs. -- ~~~~)
Line 762:
 
print(unpack(record:match"hello,how,are,you,today"))</lang>
A different solution using the string-library of Lua: (skips empty columns)
<lang lua>str = "Hello,How,Are,You,Today"
 
Line 769:
tokens[#tokens+1] = w
end
 
for i = 1, #tokens do
print( tokens[i] )
end</lang>
 
e.g. to split a string with a delimiter of | AND allowing for empty values: (NOTE: This can probably be cleaned up)
<lang lua>str = "Hello|How|Are|You||Today"
 
tokens = {}
for w in string.gmatch( str, "([^|]*)|?" ) do
tokens[#tokens+1] = w
end
table.remove(tokens)--pops off the last empty value, because without doing |? we lose the last element.
 
for i = 1, #tokens do
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.