How to find out if the first letters in 2 strings match?

I Want to Find out if the first few letters in a string match those of another string.
Thanks in advance!

is string.match used for this?

I’m on mobile, basically you can use

if string.sub(“string here”,1,2) == “XY” then

What I mean is that I need to find all of the first letters that match. Sorry if I was unclear.

You can use string.match to get the 2 characters, but I’d use string.sub in this case.

local str = 'This is an example.'
print(string.match(str, '..')) --> Th

To check if they both match then maybe use this, but it’s pretty bad.

local str = 'hhello there'
local matched = string.sub(str, 1, 1) == string.sub(str, 2, 2)
print(matched) --> true

Is string.sub and string.match is the same thing??

Nope, they aren’t the same.


So whats the diference between them.I kinda confuse…

Is this what you’re looking for? Can be used for chat commands etc.

-- Goal: compare the first word in StringA with StringB

local StringA, StringB = "My string", "My"
local FirstWord = string.split(StringA, " ")[1]
local FirstWordMatches = string.lower(FirstWord) == string.lower(StringB) --> true

I don’t think the words “match” and “sub” mean the same thing

@flkfv says that You can use string.match to get the 2 characters, but I’d use string.sub in this case. . thats why I kinda confuse

Thanks… I didnt know that this article exist

The difference with match and sub is match check’s for something with a pattern (string.find can also do that), sub gets a part of a string.

I said that because the op said if string.match could be used to do this.