I know I’m probably overlooking this situation but I am trying to compare 2 strings to each other.
I am using string.match
and it returns my string even if it doesn’t start with what I am looking for.
Example:
string.match("string", "ring")
This returns “string” even if it doesnt start with the word “ring”
I appreciate all help in advanced 
2 Likes
string.match
returns the string that it matches with another string, regardless of the order. You can instead use string.find
, which actually returns two numbers dictating the start of the term and the end. For example:
print(string.find("hello", "ell")) -- 2 4
print(string.find("hello", "hel")) -- 1 3
print(string.find("hello", "1231")) -- nil
--so...
local start, End = string.find("string", "ring")
if start == 1 then
--if "ring" is first in "string" -- which is not true
end
4 Likes
I see that there has already been a solution, but I wanted to post this as another example of how you could do this. Sometimes when you can’t find a built in function that works for you, you have to create your own. I had to do this same thing, and this is the code I came up with:
local function stringMatch(str1, str2)
if type(str1) == "string" and type(str2) == "string" then
if string.len(str1) >= string.len(str2) then
local subString = string.sub(str1, 1, string.len(str2))
if subString == str2 then
return str1
end
end
else
return error("Parameters must be strings")
end
end
Calling this function should have the same desired output you were wanting.
2 Likes