Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to check if a string contains letters/numbers?

Asked by
NiKxzsu 13
6 years ago

Lets say:

var = "8rk3pd"

Is there a way to check if the var has letters/numbers?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hi NiKxzsu,

Yes, in-fact, it's very easy. All you need to do is use the :match() function of a string and use a string pattern with it. I'll just show you how to do it to make things easier.

local var = "8rk3pd"      -- The string.  
print(var:match("%a+"))      -- Prints 'rk' because that's the first combination of letters. %a is the string pattern for letters, and the '+' sign indicates that I want a sequence of them if possible. So, it returns rk rather than just r.

Anyways, that's one way to do it. But, if you're interested in printing all of the letters from a string, you would loop through them using a for each iteration. Like:

local var = "8rk3pd" -- The string.

for i in string.gmatch(var, "%a+") do -- Will take the string and go through it to find sequences of letters. If you want to print each letter individually rather than sequences, you just need to remove the '+' sign.
    print(i) -- Prints the letter that it's iterating through.
end -- end for for each iteration.

If you want to change any of the above code to do just numbers, then you can simply change the string patterns, which can be found here.

Well, I hope I helped and have a nice day/night.

Thanks,

Best regards,

~~ KingLoneCat

Ad

Answer this question