How do I check if a character is uppercase?

ch = "A"
if isupper(ch) then
    --Press shift
end

Checked Google. Nothing was there.

Why do I need to enter more characters when I have concisely provided my question? OMFG

New contributor

Silver_Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

I solved it.

ch = "A"
if string.match(ch, "%u") then
    --Press Shift
end

This will check if a character is an uppercase letter and do whatever.

New contributor

Silver_Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

To check if a single-character string is uppercase, you could use the string.upper method like so:

Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> b = "a" == string.upper("a")
> b
false
> b = "A" == string.upper("a")
> b
true

Alternatively, if you really just want to check single characters e.g. of ASCII, you can use string.byte to get the character code and check if it matches the uppercase ASCII range (65 — 90):

> string.byte("A")
65
> string.byte("Z")
90

Note that this will become more complicated if you wish for proper Unicode awareness. However for quickly parsing keyboard input (which seems to be the use case) it might do the job.

Khám phá các thẻ bài đăng