Allow string.split to take string patterns

As a Roblox developer, it is difficult to make string.split work for string patterns. The only solution right now is to make a wrapper that does this for you but this behaviour should be built in.

My use case for this would be parsing user input messages, like for admin commands. I split the command and arguments for that command via whitespace, with string.split(message, " "). But the issue is that a player could input an arbitrary amount of spaces, thus messing up the result.

If this issue is addressed, it would improve my development experience because I wouldn’t have to worry about small things like that.


apparently they don’t want us specifying a solution but i had an idea nonetheless

string.split(string s, string delimiter = ",", bool plain = true)

kind of like how string.find works with the last argument

17 Likes

This probably was’t added because it would be seldom used.

This seems reasonably niche, but it does seem like a neat addition.

If you need this, it doesn’t seem too hard to implement.

local function split(str,by,starting,plain)
	starting = starting or 1
	if starting < 0 then
		starting = #str+starting+1
	end
	if by == "" then
		local tbl = table.create(#str-starting+1,nil)
		for i=starting,#str do
			tbl[i] = string.sub(str,i,i)
		end
		return tbl
	end
	local tbl = {nil,nil,nil,nil}
	local matchStart,matchEnd = string.find(str,by,starting,plain)
	local lstr = #str+2
	while matchStart and starting < lstr do
		table.insert(tbl,string.sub(str,starting,matchStart-1))
		starting = math.max(matchStart,matchEnd)+1
		matchStart,matchEnd = string.find(str,by,starting,plain)
	end
	table.insert(tbl,string.sub(str,starting,-1))
	return tbl
end
3 Likes

This is exactly why this should be an argument. It’s too much work. As for the use case, that was merely an example. There are far more use cases.

3 Likes

This definitely should be added, it would save developers a lot of time and has plenty of use cases. Please consider doing this sometime as it is a small feature that would go a long way.

10 Likes