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

string.upper case not detecting when the string is upper case?

Asked by
tratoi 7
6 years ago
function theissue(l)
    --yea basically this wont detect if the string is upper case or lower case i have no idea what im doing wrong here
    local typ = ""
if string.upper(l) then
    typ = "upper"
else
    typ = "lower"
end
print(typ)
end

Basically it wont detect if the string is upper case i dont know what i am doing wrong but apparently im doing something wrong i guess

0
else is not necessary DeceptiveCaster 3761 — 6y
0
for one, dont make the name of the first parameter "1", the lua parser is going to see that as a number, and a variable name fanofpixels 718 — 6y
0
thats actually the letter L User#23365 30 — 6y

2 answers

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

string.upper() only just returns the string with all the letters in uppercase, it doesnt return if theres uppercase nor lowercase letters inside the string. you'd use string.match() and the %u pattern.

local function theissue(L) -- use local functions
    local typ

    if string.match(L, "%u") then
        typ = "upper"
    else
        typ = "lower"
    end
end

though this would only detect if theres a letter thats uppercase, not the entire string. if you'd want to find out if the entire string is uppercase then you could loop through it.

local function theissue(L) 
    local typ = "upper"  

    for letter in string.gmatch(L, "%a") do 
        if not string.match(letter,"%u") then -- if it finds a lowercase letter
            typ = "lower" 
            break -- breaks the loop
        end
    end

    print(typ)
end

the %a pattern is every letter in a string.

0
thank you it works but how do i do the thingy that it says answered tratoi 7 — 6y
0
its under the answer? User#23365 30 — 6y
0
thanks tratoi 7 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Here's a function to check if it is uppercase.

function IsUpper(s)
    return (s == string.upper(s));
end
0
why the semicolons User#24403 69 — 6y
0
It's required to end a statement with it in alot of languages and is good practice to end a statement with it even if the language doesn't require it. bludud1234 40 — 6y
0
In Lua it's considered bad practice by some, but it's mostly personal preference, personally I think it looks neater but I don't tend to use it ;p turtle2004 167 — 6y
0
Yeah, it's just for neatness and to make the code easier to read, but I'd never say that it's bad practice to use them. bludud1234 40 — 6y

Answer this question