String is All Lower or All Upper Case
How do we find a string is All Lower or All Upper case characters?
There are so many ways to accomplish the same thing. We immediately go to the most simple solution; using the ToLower() method on the string object and comparing the original string input to the lower version of the same sentence. The code is shown below.
This is a simple method that should be fairly performant. We can make it great performant by using a regular expression. This is shown in the following code:
We can flip around the above regular expression to check for Upper Case.
Happy Coding...
There are so many ways to accomplish the same thing. We immediately go to the most simple solution; using the ToLower() method on the string object and comparing the original string input to the lower version of the same sentence. The code is shown below.
private bool IsAllLowerCase(string value)
{
return (value.Equals(value.ToLower()));
}
{
return (value.Equals(value.ToLower()));
}
This is a simple method that should be fairly performant. We can make it great performant by using a regular expression. This is shown in the following code:
using System.Text.RegularExpressions;
private bool IsAllLowerCase(string value)
{
// Allow anything but upper case
return new Regex(@"^([^A-Z])+$").IsMatch(value);
}
private bool IsAllLowerCase(string value)
{
// Allow anything but upper case
return new Regex(@"^([^A-Z])+$").IsMatch(value);
}
We can flip around the above regular expression to check for Upper Case.
Happy Coding...
Labels: .net2.0, .net3.5, lower case, regex, regular expression, upper case, visual studio
Post a Comment