I need help with strings

How would you get the Xth character in a string? Thank you.

local str = -- the string
local num = -- number of the character you want

print(str:split('')[num])

Use string.sub.

string.sub(yourString,3,3)
(3 being the position)

1 Like
function GetNthCharacter(String, Number)
    return string.match(String, string.rep(".", Number - 1).."(.)") or ""
end

for Count = 1, 5 do
    print(GetNthCharacter("12345", Count))
end

this way is longer then the other two solutions, but I decided to share it

this worked, thank you for your help

1 Like

I appreciate it, but I only need this for something small, your code would be overdoing it lol

1 Like