Help with string manipulation

How would I split a string at the first space? I don’t want to split the string at all the spaces, only the first.
For example, the string

'This is an example'

would be

{'This', 'is an example'}

Thanks for any help!

You could use string.split to split on all spaces, then just add the others back together. This is probably not the best solution but if you wanted you could probably to some voodoo with string.format (string.format is witch-craft)

I think you meant string.match.
If you wanted to use string.match, you could do this:

local str = "This is an example"
local result = {string.match(str, "([^ ]+) (.+)")}

Yeah I have not really experimented with that string manipulation stuff because it looks so intimidating, hence why I called it voodoo.

This would be a good thing to look at if you need help with strings!

local str = "Socket to Him." 
local v1 = str:split(" ") 
local splitstr = v1[1] 
local fullstr = table.concat(v1, " "):gsub(splitstr, "") 
print({splitstr, fullstr}) --[[ {
                    [1] = "Socket",
                    [2] = " to Him."
                 }  --]]

@alksjdfl method also works.

The gsub is unnecessary.

local str = "Socket to Him."
local values = string.split(str, " ")
local result = {values[1], table.concat(values, " ", 2)}

Or if you don’t want to create a new table,

local str = "Socket to Him."
local result = string.split(str, " ")
result[2] = table.concat(result, " ", 2)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.