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

How would I ignore casing?

Asked by
unmiss 337 Moderation Voter
8 years ago

I understand you can use string:lower() or string.upper(), but that's not exactly what I'm trying to do. Example:

local var = "BRIGHT BLUE"

Now, the correct capitalization is "Bright blue" What I've noticed is that in Java you can use EqualsIgnoreCase(). Is there a similar function?

Yes, I did search the site for this. I don't see it. No downvotes please...

1 answer

Log in to vote
3
Answered by 8 years ago

You can compare two strings without case by using the lower or upper method on both of them.

print(("bob"):lower() == ("BOB"):lower())

To create a method.

In regular Lua you could modify string

string.equalsIgnoreCase = function(self,other)
    return self:lower() == other:lower()
end

print(("Hello"):equalsIgnoreCase("HELLO"))

Since Rbx.Lua doesn't allow modifications to string. The closest you will get to this in Rbx.Lua is:

local NewString = {}
for o,n in pairs(string) do
    NewString[o] = n
end
local E = getfenv()
E.string = NewString
setfenv(1,E)

string.equalsIgnoreCase = function(one,two)
    return one:lower() == two:lower()
end

print(string.equalsIgnoreCase("BOB","bob"))

Or just a function

equalsIgnoreCase = function(one,two)
    return one:lower() == two:lower()
end

print(equalsIgnoreCase("BOB","bob"))
Ad

Answer this question