Is there a built-in IsLowerCase()
in .NET?

- 57,289
- 29
- 176
- 237

- 222,824
- 274
- 634
- 905
-
The implementation's only trivial until you need to consider other locales... – Roger Lipscombe Dec 23 '08 at 15:19
-
Is this for a string or just a char? – BenAlabaster Dec 23 '08 at 15:30
8 Answers
public static bool IsLowerCase( this string text ) {
if ( string.IsNullOrEmpty( text ) ) { return true; }
foreach ( char c in text )
if ( char.IsLetter( c ) && !char.IsLower( c ) )
return false;
return true;
}
"someString".IsLowerCase();

- 25,849
- 11
- 66
- 104
Keep in mind that localization makes this a non-trivial question. The first example is fine as long as you don't care:
string s = ...
s.All(c => char.IsLower(c));
If you do care, do it this way:
s.ToLower(CultureInfo.CurrentUICulture) == s;
This gives you the chance to address culture issues.

- 45,157
- 15
- 111
- 168
-
-
dalle: You need to do a String.ToCharArray() before you can do lambda expressions on characters. That is, bool isStringLower = str.ToCharArray().All(c => char.IsLower(c)); – Tamas Czinege Dec 23 '08 at 16:35
-
@DrJokepu: Actually, you don't need to do ToCharArray() before you can do the lambda - I just tested it, it works fine on a string too... – BenAlabaster Dec 23 '08 at 17:43
-
@DrJokepu: According to http://msdn.microsoft.com/en-us/library/system.string_members.aspx#extensionMethodTableToggle you don't need it. – dalle Dec 24 '08 at 00:02
-
Ahh, I think IntelliSense just supresses them on string. I'll modify my answer. – Jay Bazuzi Dec 24 '08 at 01:08
Edit: Didn't see the actual meaning of your question. You could use:
char.IsLower(c);
As far as easily converting between cases:
Sure is:
string upper = "CONVERTED FROM UPPERCASE";
Console.WriteLine(upper.ToLower());
It's part of the string
class.
There's also the TextInfo
class:
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
Console.WriteLine(textInfo.ToTitleCase(title));
Console.WriteLine(textInfo.ToLower(title));
Console.WriteLine(textInfo.ToUpper(title));
Which allows for more variation to change caps and whatnot (like ToTitleCase
).

- 57,289
- 29
- 176
- 237
-
I think what he is asking is if there is a function that identifies a lower case string – Sergio Dec 23 '08 at 15:33
As others have mentioned you can easily do this for a single char using char.IsLower(ch)
But to extend the String primitive, it wouldn't be very difficult. You can extend the BCL relatively simply using the Runtime.CompilerServices namespace:
Imports System.Runtime.CompilerServices
Module CustomExtensions
<Extension()> _
Public Function IsLowerCase(ByVal Input As String) As Boolean
Return Return Input.All(Function(c) Char.IsLower(c))
End Function
End Module
Or in C#, that would be:
using System.Runtime.CompilerServices;
static class CustomExtensions
{
public static bool IsLowerCase(this string Input)
{
return Input.All(c => char.IsLower(c));
}
}
Now you can figure it out using:
Console.WriteLine("ThisIsMyTestString".IsLowerCase())
Which would return false because there are upper case characters contained within the string.

- 39,070
- 21
- 110
- 151
-
simply "return Input.All(c => char.IsLower(c))" is enough, and faster since it can return as soon as it finds the first upper case letter. – Lucas Dec 23 '08 at 16:11
-
Ah, nice...I never noticed you could do an All on a String object... thanks. – BenAlabaster Dec 23 '08 at 17:40
How about:
public bool IsLower(string TestString)
{
if (String.IsNullOrEmpty(TestString))
{
return true;
}
string testlower = TestString.ToLowerInvariant();
if (String.Compare(TestString, testlower, false) == 0)
{
return true;
}
else
{
return false;
}
}

- 4,117
- 2
- 26
- 18
-
Long windedness is intentional in this case. And yeah, i think asd234as!!!df is lower case. 2,3,4 and ! by definition don't have case at all so are both lower and upper case. – Stever B Dec 23 '08 at 16:51
balabaster, please do not use this approach with FindAll/Count. All you need is
return Input.ToList().Exists(c => Char.IsUpper(c));
It will stop the iteration on the first upper case character.FindAll create a new List and you use only the Count property. If we have a long string that's in upper case, you will end up with a copy of the original string.

- 2,729
- 2
- 22
- 17
-
@Petrov: .All (as I have used) drops out on the first existence of a non-lowercase character. What you've suggested is equally long winded. If you drop the ToList().Exists() and use instead just .All(c => char.IsLower(c)) then you get even better results! – BenAlabaster Dec 23 '08 at 17:48
Guys why this LINQ abuse (ToList(), ToArray(), All(), Any(), ...) ? I love LINQ and lambdas too but in this case I think the good old foreach is what we need. See the answer of TcKs as reference - but we can do better if we remove the superfluous
char.IsLetter( c )
because IsLower() is doing the same check.

- 2,729
- 2
- 22
- 17
-
Because a nice .All(c => Char.IsLower(c)) takes care of the whole lot... forget iterating over a collection - just query it like you would a table in a database. Much more elegant... – BenAlabaster Dec 23 '08 at 17:46