Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a string contains uppercase characters

Tags:

string

c#

regex

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

like image 378
QFDev Avatar asked Nov 17 '13 15:11

QFDev


People also ask

How do you check if a string has an uppercase letter in Java?

isUpperCase(char ch) determines if the specified character is an uppercase character. A character is uppercase if its general category type, provided by Character.

How do you check if a string contains a capital letter in Python?

Python Isupper() To check if a string is in uppercase, we can use the isupper() method. isupper() checks whether every case-based character in a string is in uppercase, and returns a True or False value depending on the outcome.

How do you check if any character in a string is uppercase in C#?

IsUpper(String, Int32) Method. This method is used to check whether the specified string at specified position matches with any uppercase letter or not. If it matches then it returns True otherwise returns False.

How do you know if an element is uppercase?

You can also use a regular expression to explicitly detect uppercase roman alphabetical characters. isUpperCase = function(char) { return !!/[A-Z]/.


2 Answers

You can use LINQ:

fullUri.Any(char.IsUpper); 
like image 62
MAV Avatar answered Sep 30 '22 03:09

MAV


RegEx seems to be overkill:

bool containsAtLeastOneUppercase = fullUri.Any(char.IsUpper); 
like image 40
nvoigt Avatar answered Sep 30 '22 02:09

nvoigt