✓ Accepted answer
local n = "John Smith"
local name = {}
function divideName(t)
local p1 = t:match("%a+ ")
if p1 then
p1 = p1:sub(1,p1:len() - 1)
local pos = t:match("%a+ ")
local len = pos:len() + 1
table.insert(name,p1)
local p2 = t:sub(len)
if p2 then
table.insert(name,p2)
end
end
end
divideName(n)
for _,v in pairs (name) do
print(v)
end
Currently, this only works for 2 words. Any more, and the table will be "John, Smith Kyle etc."
Let me explain:
I made it a function for ease of use. I matched the first word, and removed the space at the end (the space was to make sure it was the first word).
Next, I matched the first word again, and then I got the position of the next word (the value len).
After that, I put p1 into the table.
I defined p2 as the next word, and everything after it using len.
Finally, I inserted it into the table as the last word.
The for loop at the bottom of script is for debugging, it prints every value of the name table.
For references on string manipulation and/or patterns; http://wiki.roblox.com/index.php?title=Function_dump/String_manipulation http://wiki.roblox.com/index.php?title=String_pattern
Hope I helped :)
~TDP
1 comment
This may be relevant http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/