Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if a string is all CAPS

Tags:

string

c#

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)

like image 584
StubbornMule Avatar asked Jan 15 '09 19:01

StubbornMule


People also ask

How do you know if a string begins with uppercase?

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.

How do you check if a letter in a string is uppercase Python?

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”.

How do you know if a letter is uppercase?

isupper() – check whether a character is uppercase.


1 Answers

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;     } 
like image 101
Greg Dean Avatar answered Oct 18 '22 14:10

Greg Dean