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:
<
You can use LINQ:
fullUri.Any(char.IsUpper);
RegEx seems to be overkill:
bool containsAtLeastOneUppercase = fullUri.Any(char.IsUpper);
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
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;
}
}
using for loops, not as efficient and readable as the other methods pointed out, but for starters should work and provide a comprehensive way of doing this:
int counter = 0;
for(int i=0; i< myString.Length;i++)
{
//if character is upper add +1 to counter
if(char.IsUpper(chaineNonPascale[i]))
{
counter++;
}
}
Basically, you iterate over your string and check for Upper Chars, then you can add logic as to what to do with the place where there is an Upper Char. For example, insert a space where the second upper case char is found and then use the ToLower method on the whole string...
Use Linq!
fullUri.Any(c=> char.IsUpper(c));