64

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

3
  • you may compare string with its lower-case equivalent Nov 17, 2013 at 15:28
  • @johny: That is a lot more work than simply looking for the first uppercase character you can find in the string... Nov 17, 2013 at 15:28
  • @Johny, this works, but it creates a second string in memory and does a string comparision. Using a regex or iterating over the string in this case is probably the better approach performance-wise...
    – user2819245
    Nov 17, 2013 at 15:29

8 Answers 8

129

You can use LINQ:

fullUri.Any(char.IsUpper);
0
27

RegEx seems to be overkill:

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

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

11

Use LINQ!

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

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

3
  • How is this an answer to the question "Is there an alternative to using a regular expression"? Nov 17, 2013 at 15:29
  • 1
    I can be sure that all my paths will be free of non-ascii characters but definitely useful for others!
    – QFDev
    Nov 17, 2013 at 15:29
  • 1
    @MartinSmith: Uh, yes, you're right. I was thinking along the lines of "How can this line of code be improved?", so this is not really an answer to the question (but may be worth considering anyway, so I'll leave it in case someone's interested). Nov 17, 2013 at 15:30
1

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

1

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;
    }
}
1
  • Re "Using LINQ might have an impact on performance": That could be, but do you have a reference and/or a measurement? Can you quantify it? Jul 12 at 19:39
-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;
2
  • Instead while (enumerator.MoveNext()) { return true; } return false; you could just return enumerator.MoveNext(); May 28, 2022 at 20:49
  • Sadly, no. An expression is not possible, because you have other stuff happening in the same scope. Jun 2, 2022 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.