Is there a built-in IsLowerCase()
in .NET?
views:
568answers:
10Edit: 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
).
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();
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.
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;
}
}
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.
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.
Please Stop using ToUpper() and ToLower() ! These methods will create a copy of the original string !
you can read my blog post on the subject http://ppetrov.wordpress.com/2008/06/27/dont-use-toupper-or-tolower/
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.