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;
}
I like the LINQ approach.
If you want to restrict it to all upper case letters (i.e. no spaces etc):
return input.All(c => char.IsUpper(c));
or using a method group conversion:
return input.All(char.IsUpper);
If you want to just forbid lower case letters:
return !input.Any(c => char.IsLower(c));
or
return !input.Any(char.IsLower);