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.
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.
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.
The includes() method returns true if a string contains a specified string. Otherwise it returns false .
[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.
What about:
//true if it doesn't contain letters bool result = hello.Any(x => !char.IsLetter(x));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With