Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a String contains any letter from a to z? [duplicate]

Possible Duplicate:
C# Regex: Checking for “a-z” and “A-Z”

I could just use the code below:

String hello = "Hello1"; Char[] convertedString = String.ToCharArray(); int errorCounter = 0; for (int i = 0; i < CreateAccountPage_PasswordBox_Password.Password.Length; i++) {     if (convertedString[i].Equals('a') || convertedString[i].Equals('A') .....                             || convertedString[i].Equals('z') || convertedString[i].Equals('Z')) {         errorCounter++;     } } if(errorCounter > 0) {     //do something } 

but I suppose it takes too much line for just a simple purpose, I believe there is a way which is much more simple, the way which I have not yet mastered.

like image 977
Hendra Anggrian Avatar asked Oct 14 '12 17:10

Hendra Anggrian


People also ask

How do you check if a string contains any letter from A to Z?

To check if a string contains any letter, use the test() method with the following regular expression /[a-zA-Z]/ . The test method will return true if the string contains at least one letter and false otherwise.

How do I check if a string has repeated characters?

If we want to know whether a given string has repeated characters, the simplest way is to use the existing method of finding first occurrence from the end of the string, e.g. lastIndexOf in java. In Python, the equivalence would be rfind method of string type that will look for the last occurrence of the substring.

How do you check if a string contains some letters?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

How do you check that a string contains only AZ AZ and 0 9 characters?

[A-Za-z0-9] matches a character in the range of A-Z, a-z and 0-9, so letters and numbers. + means to match 1 or more of the preceeding token.


1 Answers

What about:

//true if it doesn't contain letters bool result = hello.Any(x => !char.IsLetter(x)); 
like image 171
Omar Avatar answered Sep 22 '22 05:09

Omar