[code]
– splits a string by a pattern
– returns an array of the pieces
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
[/code]
str = 'UserID="world", UserName="Lua"'
print("Value for str:" .. str)
for k,v in string.gmatch(str, '([%w]+)="([%w]+)"') do
print("Key:" .. k .. " Value:" .. v);
end
When executed will result in:
Value for str:UserID="world", UserName="Lua"
Key:UserID Value:world
Key:UserName Value:Lua
[quote=“shicks3, post:1, topic:175132”]I would like to extract the ID and the UserName from the string returned from the Kwikset door lock:
UserID=“world”, UserName=“Lua”[/quote]
A more targeted version than hek’s or RichardTSchaefer’s (i.e., you won’t learn as much from mine but you’ll get what you want quicker):
str='UserID="world", UserName="Lua"'
i,n = str:match('^UserID="(.-)", UserName="(.-)"$')
-- use i (UserID) and n (UserName).