Search

Check if all upper case string

This C# code snippet checks to see if the specified string is all upper case, that is, contains no lower case characters.

using System.Text.RegularExpressions;

const string ALL_CAPS_PATTERN = "[a-z]";
 
static readonly Regex All_Caps_Regex = 
   new Regex (ALL_CAPS_PATTERN);
 
static bool AllUpperCase (string inputString)
{
   if (All_Caps_Regex.IsMatch ( inputString ))
   {
      return false;
   }
   return true;
}
Below is another approach:
using System.Text.RegularExpressions;

static bool AllCapitals(string inputString)
{
   return Regex.IsMatch(inputString, @"^[A-Z]+$");
}