Detecting Uppercase Letters

I’ll keep this short.

Let’s say you have a basic string from a player’s message, for example:

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        --message
    end)
end)

Now, I wanted to then check how many uppercase letters are in that string.

Well, that’s the thing, how do I check the amount of uppercase letters in a string?

I can already turn it into a percentage, I just want to simply detect an uppercase letter;

local str = "Hello World!"
local uppercase = 2 --If I somehow got that amount.

local percentage = math.round(uppercase/str:len())*100
--15.38%

Please let me know. Thank you.

Loop through each letter, and check if the letter is equal to itself converted to lowercase. Something like this:

local message = "aBcDeF"
local numberOfUppercaseLetters = 0
for i = 1, #message do
    local character = message:sub(i,i)
    if not (character == character:lower()) then
        --Character is uppercase!
        numberOfUppercaseLetters  += 1
    end
end
print("Message has",numberOfUppercaseLetters,"uppercase letters.")

1 Like

What if you lowercase an already lowercased letter, it’ll be the same.

Nevermind, that’s not the issue. It should work.

1 Like

Using this, I can try to make an anti-capital system.

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        
        local upperCase = 0
        for i = 1,message:len() do
            if not message:sub(i,i) == message:sub(i,i):lower() then
                upperCase += 1
            end
        end
        
        if upperCase >= 6 then
            --Punish the player somehow
        end
        
    end)
end)

Thank you for helping, @Exarpo.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.