How To Find Where A Specific Character In A String Is

I’m looking for a way to find where a letter is inside a word. A combination of string.Match & string.len.
There seems to be no function or parameter to do this. Can’t think of a solution either.
For example,

local word = "Octopus"
function letterfind(word)
local letter = p
local place = 5
return (letter,place)
end
letterfind(Octopus)

hmm string.find i think that will return where it is

You could use string.sub if you want to get a character at certain index.

For e.g:

local word = "Hi"
print(word:sub(1)) --> "H"
print(word:sub(4)) --> empty
3 Likes

string.sub with only two arguments (the string and the starting position) assumes that the third argument is -1 which is equivalent to the string’s length.

The second example won’t return nil, it’ll return an empty string instead.

1 Like

That works perfectly. Thank you :smiley:

1 Like

Thank you for the correction of the small note.