Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the fastest way to check whether string has uppercase letter in c#?

Tags:

My first implementation idea is to do simply:

bool hasUpperCase (string str) {     if(string.IsNullOrEmpty(str))          return false;     for (int i = 0; i < str.Length; i++) {         if (char.IsUpper (str[i]))             return true;                         }     return false; } 

but maybe there is another faster way to do that ?

like image 314
tomaszkubacki Avatar asked Jun 01 '11 01:06

tomaszkubacki


People also ask

How do you check if a letter in a string is uppercase C?

C isupper() The isupper() function checks whether a character is an uppercase alphabet (A-Z) or not.

How do you check if all strings are uppercase?

The string isupper() method returns True if all cased characters in a string are uppercase. Otherwise, it returns False . If the string doesn't contain any cased characters, the isupper() method also returns False .

How do you check if a string is all lowercase?

The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

Which expression can be evaluated to determine whether a character ch is an uppercase alphabet?

The isupper() function checks if ch is in uppercase as classified by the current C locale. By default, the characters from A to Z (ascii value 65 to 90) are uppercase characters.


1 Answers

You could reduce that to

bool HasUpperCase (string str) {     return !string.IsNullOrEmpty(str) && str.Any(c => char.IsUpper(c)); } 

using LINQ.

like image 131
Femaref Avatar answered Oct 23 '22 04:10

Femaref