Log in to vote
0

How to detect if somebody used a letter or not?

Asked by
blowup999 562 Moderation Voter
7 years ago

Is it possible to detect if the first 'letter' somebody said was a letter or something else like a 9 or a :?

1 answer

Log in to vote
2
Answered by 7 years ago

Yes, you can use the string.sub function to slice strings in order to compare parts of them.

msg = ":m hello"

if msg:sub(1, 1) == ":" then
    print(msg:sub(2))
    --^ prints "m hello"
end

Edit: Checking if a string is a letter

function is_letter(s)
    if not s:match("[^%a]") then
        return true
    end
end

print(is_letter(msg:sub(1, 1)) and "It's a letter" or "Not a  letter!")
--> Not a letter!

print(is_letter("a") and "It's a letter!" or "Not a letter!")
--> It's a letter!
0
But how do you detect if its any letter at all and without using 26 lines? blowup999 562 — 7y
0
print(msg:sub(1, 1)) --> : User#11893 186 — 7y
0
But how do I do it without doing if msg:sub(1,1)=="LETTER" then 26 times to make sure its a letter? blowup999 562 — 7y
0
I've updated the answer User#11893 186 — 7y
Ad

Answer this question