This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DrLancer's profile picture

[Lua] toLower toUpper?

Started by DrLancer, 22 January 2013 - 03:56 AM
DrLancer #1
Posted 22 January 2013 - 04:56 AM
Hi all.

Is it possible to use a function similar to toLower(string) to ex. compare 2 strings - no matter if it's upper/lower-case or a mix of both?
GravityScore #2
Posted 22 January 2013 - 05:03 AM
There currently isn't a function in the string library that can compare whether a string is uppercase or lowercase, but it's really easy to do. (I assume what you ment was you were wanting to check if a string was completely uppercase or lowercase).


s = "your MuLtIcAsE string here..."


if string.lower(s) == s then
  -- It is all lowercase
elseif string.upper(s) == s then
  -- It is all uppercase
end

-- OR using a different notation:
if s:lower() == s then
  -- It is all lowercase
elseif s:upper() == s then
  -- It is all uppercase
end
remiX #3
Posted 22 January 2013 - 05:05 AM
A function to check if two strings are the same?


stringToCompare = "hello"

function checkIgnoreCase( yourString, checkString )
    return string.lower(yourString) == string.lower(checkString)
end

print("Type hello!")
input = read()

if checkIgnoreCase( input, stringToCompare ) then
    print("Yes you wrote " .. stringToCompare)
else
    print("No what crap did you write? " .. input)
end

Basically, you just string.lower(string) both things you are checking to see if they're equal.
DrLancer #4
Posted 22 January 2013 - 06:40 AM
Thanks that's exactly what i was looking for.
- I now notice that i explained my problem a bit wrong, as it wasn't the actual compare function i was looking for, but simply if i could force a string to BECOME lower/uppercase.

I forgot when i needed it, but i just came to think of it again :)/>

I usually do it with user inputs, if they have to type "yes" and type "yEs" then i assume it wouldn't accept it as it's not EXACTLY the same.
So in my last code, i had to type eg.
if myString == "yes" or myString == "YES" or ...
which becomes very tedious in the long run ;)/>
remiX #5
Posted 22 January 2013 - 07:01 AM
Yeah, with the function I had you would use it like this:


if checkIgnoreCase(myString, "yes") then
-- if it's equal
else
-- if it's not equal
end