Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I separate a string into a table?

I am trying to figure out how to make my script to convert a name (string) into a table. For example, if a string is named"John Smith", then it would be converted to this:

name={"John", "Smith"}

basically sorting people's last names from their first name. I'm really stuck though, I've read the wiki page on string and pattern manipulation and I am still having trouble figuring out how to do this. Could you guys help?

1 comment

2 answers

✓ 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

0

Really helpful and informative, thanks!

To complement TheDeadlyPanther's answer, you can simply use gsub to cap everything that is not a space.

local function split(s,d)
	d = d or ' '; -- Delimiter, single character or set. Defaults to space
	local t = {};
	s:gsub('([^'..d..']+)',function(c) t[#t+1] = c end);
	return t;
end

The same function using gmatch:

local function split(s,d)
	d = d or ' ';
	local t = {};
	for c in s:gmatch('([^'..d..']+)') do
		t[#t+1] = c;
	end;
	return t;
end;

How does it work?

Gsub and gmatch are both string manipulation functions that capture patterns. Gsub takes a second argument, which is either a function (With all captures passed as arguments) or a string to replace the matched string with. Gmatch returns an iterator to use in for loops.
The [^ ]+ bit is a pattern that matches everything but a space. The [] denotes a set, the ^ inverts it and the + matches 1 or more cases of it (greedy).

1 comment

0

This is definitely far cleaner than rolling-your-own when it works. If you only need to split by whitespace, %S+ is a little easier to read than [^ ]+

This discussion is preserved as it was. New answers, comments, and votes are closed.