In C# is there a way to detect if a string is all caps?
Most of the strings will be short(ie under 100 characters)
Assuming that words in the string are separated by single space character, split() function gives list of words. Secondly to check if first character of each word is uppercase, use isupper() function.
isupper() In Python, isupper() is a built-in method used for string handling. This method returns True if all characters in the string are uppercase, otherwise, returns “False”.
isupper() – check whether a character is uppercase.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With