QFDev
QFDev

Reputation: 8988

Detect if a string contains uppercase characters

Is there an alternative to using a regular expression to detect if a string contains uppercase characters? Currently I'm using the following regular expression:

Regex.IsMatch(fullUri, "[A-Z]") 

It works fine but I often hear the old adage "If you're using regular expressions you now have two problems".

Upvotes: 68

Views: 93895

Answers (9)

waawaaa
waawaaa

Reputation: 1

    public static bool containsCapital(string password)
    {
        var chars = password.ToCharArray();

        foreach(char ch in chars)
        {
            if(char.IsUpper(ch))
            {
                return true;
            }
        }

        return false;
    }

Upvotes: 0

Mikhail
Mikhail

Reputation: 1

Use:

string text = "Hello, World!";
char[] Letters = text.ToCharArray();
IEnumerable Sentence = from l in Letters where char.IsUpper(l) select l;
IEnumerator enumerator = Sentence.GetEnumerator();
while (enumerator.MoveNext())
{
    return true;
}
return false;

Upvotes: -1

SaiKanth_K
SaiKanth_K

Reputation: 163

Using LINQ might have an impact on performance when using a large string. You can also use ASCII-level comparison.

byte[] asciiBytes = Encoding.ASCII.GetBytes(fullUri);
for (int i = 0; i < asciiBytes.Length; i++)
{
    if (asciiBytes[i] > 64 && asciiBytes[i] < 91)
    {
        return true;
    }
}

Upvotes: 1

glls
glls

Reputation: 2403

Using for loops, it not as efficient and readable as the other methods pointed out, but for starters it should work and provide a comprehensive way of doing this:

int counter = 0;
for(int i=0; i< myString.Length; i++)
{
    // If the character is uppercase, add +1 to the counter
    if(char.IsUpper(chaineNonPascale[i]))
    {
        counter++;
    }
}

Basically, you iterate over your string and check for uppercase characters. Then you can add logic as to what to do with the place where there is an uppercase character. For example, insert a space where the second upper case character is found and then use the ToLower method on the whole string...

Upvotes: 1

Alberto
Alberto

Reputation: 15941

Use LINQ!

fullUri.Any(c => char.IsUpper(c));

Upvotes: 12

MAV
MAV

Reputation: 7457

You can use LINQ:

fullUri.Any(char.IsUpper);

Upvotes: 138

geedubb
geedubb

Reputation: 4057

You could probably also do (if you want something that will work in .NET 1.0 :):

bool hasUpperCase = !fullUri.ToLower().Equals(fullUri);

Although a regex this simple will probably work fine

Upvotes: 11

nvoigt
nvoigt

Reputation: 77285

RegEx seems to be overkill:

bool containsAtLeastOneUppercase = fullUri.Any(char.IsUpper);

Upvotes: 27

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

Your regex will only find ASCII uppercase letters. Conveniently, the .NET regex engine is Unicode-aware, enabling you to do

Regex.IsMatch(fullUri, @"\p{Lu}") 

although I suppose that in your case you're not expecting non-ASCII letters in your string (considering its name).

Upvotes: 5

Related Questions