-2

could someone telle me please how to check if a string contains at least one alphabetical letter? I tryed:

if (StringName.Text.Contains(Char.IsLetter()))
{
 //Do Something
}

But it doesn't work.

Sardar Agabejli
  • 423
  • 8
  • 32

2 Answers2

5

You can use LINQ:

if (StringName.Text.Any(Char.IsLetter))
{
    // Do something
}
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • 3
    Very strange that no one come up with anything close to this answer before you... Maybe [another universe](https://stackoverflow.com/a/12884682/477420)... – Alexei Levenkov Oct 04 '17 at 15:17
3

Try Linq. If you accept any Unicode letter, say, Russian ъ:

if (StringName.Text.Any(c => char.IsLetter(c))) 
{
    // Do Something
}

In case you want just a..z as well as A..Z:

if (StringName.Text.Any(c => c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) 
{
    // Do Something
}

Finally, if you insist on regular expressions:

if (Regex.IsMatch(StringName.Text, @"\p{L}")) 
{
   // Do Something 
}  

Or (second option) a..z as well as A..Z letters only

if (Regex.IsMatch(StringName.Text, @"[a-zA-Z]")) 
{
   // Do Something 
}  
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215