How to split a ":" in a string?

I’ve found a past DevForum post but I can’t find out how to make the code work.

The example was using this code:

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)

I still can’t figure out how to only get the “user”.

The default implementation of string.split returns an array of all the characters between each instance of the delimiting character (in this case “:”). The gist of the example you linked is that if the input contains the delimiting character as a valid part of the input, then the results will get messy.

In the example above, string.split(str,":")[1] would give you “Builderman,” but the full table returned by that call would have broken up “Builderman’s” message into two pieces at the colon mid-message, which would have been undesired in that developer’s use case.

1 Like

In this case, user is the username you supplied before the colon. That’s surely not what you’re asking though, right? What, specifically, do you want to do? Ignore the post you found (though thank you for supplying it) and just show me what you want the input to be and what you want to get out of it.