Detecting if a string is all CAPS

No need to create a new string:

bool IsAllUpper(string input)
{
    for (int i = 0; i < input.Length; i++)
    {
        if (!Char.IsUpper(input[i]))
             return false;
    }

    return true;
}

Edit: If you want to skip non-alphabetic characters (The OP’s original implementation does not, but his/her comments indicate that they might want to) :

   bool IsAllUpper(string input)
    {
        for (int i = 0; i < input.Length; i++)
        {
            if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i]))
                return false;
        }
        return true;
    }

Leave a Comment