Checking whether string starts with the letters I enter

The title says it all… how would I check whether a string starts with the letters I enter?

1 Like

You can use string.sub method for this:

local myString = "LABC"
local firstLetter = string.sub(myString, 1, 1) -- Extract first letter from myString.

if firstLetter == "L" then --Checks if firstLetter is "L"
  print("hi")
end
2 Likes

I don’t want to check the first letter though, I want to check whether the word starts with the letters I enter. For example if I enter LE and the string I want to check it to is LABC, I don’t want it to match.

I hope that makes sense.

string.match should work.

local str = "Hello World"
local input = "Hel"

local match = string.match(str, "^"..input)

if match then
	print("matches")
else
	print("doesnt match")
end
3 Likes

This works like a charm, thanks a lot!

2 Likes

Umm, this is simply just an example, not the full script, where are you supposed to be typing the word?

I’m making an admin console but I have my answer now. Thanks though.

1 Like

 local myString = "LABC"
local chosen = "LA"
local firstLetter = string.sub(myString, 1, #chosen) -- Extract first letter from myString.

if firstLetter == chosen then 
  print("hi")
end