Splitting a String

Hey all! I hope you’re keeping safe during this epidemic. I’ve tried searching for an answer to my question but haven’t found one yet.

I want to split a string which would be username:reason by the colon and then put this into two arguments to use them later, how would I do this?

Side-note: the reason part won’t just be a word, it’ll be a sentence or so.

13 Likes

You can use string.split(string, split) for this. In your case you would want to use string.splt(“username:reason”, “:”). This will return a table of values containing what you need.

4 Likes

How will this “table of values” be returned and how can I put them into arguments? Apologies, I am quite new to coding in lua. Side-note: you spelt split as splt

You can use this:

local username, reason = unpack(("username:reason"):split(":"))

Essentially, string.split just returns the values in an array, so you can iterate over the table:

local tbl = ("username:reason"):split(":")

for _, v in pairs(tbl) do
    print(v) -- prints "username", then "reason"
end
5 Likes
local Message = "1:2" -- // this is the message you want to split
local SplitMessage = string.split(Message, ":") -- // the second arguement is where you want to split your message.
local Value1, Value2 = SplitMessage[1], SplitMessage[2] -- // since split message returned an array, you can use array[1] and array[2] to get the values of the array.
print(Value1, Value2) -- // Prints out 1 and 2.
11 Likes

Does this also join everything, for example, if I had 1:2 and 3 would it just return 2 or 2 and 3 as that’s what I am looking for.

SplitMessage[2] refers to the where the string was split between. If there was more text after “2” in the Message variable SplitMessage[2] would also return that. Adding more “:”'s to the Message variable will allow you use split the message into more values. So yes, if you had "1:2 and 3" as your string it would return "2 and 3". However if you had 1:2:3 as your string SplitMessage[2] would only return 2.

2 Likes

It would return 2 and 3 as it’s splitting the string at the “:”

Thanks, you’ve been a huge help!

String.split is great, but also highly recommend you read up on Lua patterns

e.g.

local str = 'Builderman:like patterns'
str:gsub('(.*):(.*)', function (user, reason)
    print(
        ('The user \'%s\' would like you to know that they \'%s\''):format(user, reason)
    ) --[!] Prints: The user 'Builderman' would like you to know that they 'like patterns'
end)

Why should you care? You’ll be able to have more control over what you interpret from the string. For example, if you passed ‘Builderman:like patterns:) really important message here btw!’ to the above code or to string.split you would get:

--> In the case of string.split:
local str = 'Builderman:like patterns:) really important message here btw!'

local split = str:split(':')
local user = split[1] -- Would be: 'Builderman'
local reason = split[2] -- Would be: 'like patterns'

--> In the case of the gsub above
str:gsub('(.*):(.*)', function (user, reason)
    --[!] user would be 'Builderman:like patterns'
    --[!] reason would be ') really important message here btw!'
end)

As you can see, neither of these are perfect because we miss the ‘really important message’, but there’s no easy way to solve this issue in string.split unless you sanitise or only look for the first colon, concatenating the rest of the values. However, with pattern matching…

We know that Roblox usernames only contain alphanumeric characters, so we can alter our pattern matching accordingly:

local str = 'Builderman:like patterns:) really important message here btw!'
str:gsub('(%a*):(.*)', function (user, reason)
    --[!] User is now defined as 'Builderman'
    --[!] Reason is now defined as ') really important message here btw!'

    print(
        ('The user \'%s\' would like you to know that they \'%s\''):format(user, reason)
    ) --[!] Prints: The user 'Builderman' would like you to know that they 'like patterns:) really important message here btw!'
end)

Voila, we have now avoided a potential disaster (dependent on what your code is being used for). Always look for edge cases!

31 Likes

I actually never thought of that, thanks!

2 Likes