tags:

views:

568

answers:

10

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

+6  A: 

Do you mean Char.IsLower(ch); ?

m3rLinEz
+2  A: 

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:

MSDN says:

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

George Stocker
I think what he is asking is if there is a function that identifies a lower case string
Sergio
Yup, I added that as an edit before you commented. :-)
George Stocker
+6  A: 
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();
TcKs
+1  A: 

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.

BenAlabaster
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
Ah, nice...I never noticed you could do an All on a String object... thanks.
BenAlabaster
A: 

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;
        }


    }
Stever B
A bit long winded...
BenAlabaster
also, is "asd234as!!!df" lowerCase?
Jimmy
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
+2  A: 

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.

Jay Bazuzi
Why not s.All(c => char.IsLower(c))?
dalle
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));
DrJokepu
@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
@DrJokepu: According to http://msdn.microsoft.com/en-us/library/system.string_members.aspx#extensionMethodTableToggle you don't need it.
dalle
Ahh, I think IntelliSense just supresses them on string. I'll modify my answer.
Jay Bazuzi
A: 

Check here @ MSDN.

Eppz
A: 

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.

Petar Petrov
@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
A: 

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/

Petar Petrov
I want to vote you down for yelling and for stating the obvious. I want to vote you up for the reminder.Note: ToUpper and ToLower aren't necessarily evil because they make a copy.
Stever B
A: 

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.

Petar Petrov
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