To match an entire string of English letters, use LINQ or regex:
var hasAllEnglishLetters = x.All(c => (c >= 65 && c <= 90) || c >=97 && c<= 122)); var hasAllEnglishLetters = Regex.IsMatch(x, @"^[a-zA-Z]+$");
To combine words within a larger line, you can also use regex or LINQ approaches:
var s = "Match word but not word1, w1ord or word!"; var res_linq = s.Split().Where(x => x.All(c => (c >= 65 && c <= 90) || c >=97 && c<= 122)); Console.WriteLine(string.Join(";", res_linq));
Watch the C # online demo
Details of the LINQ approach: with Split() string is broken into pieces of characters without spaces, and .All(c => (c >= 65 && c <= 90) || c >=97 && c<= 122) ensures that only these fragments that belong to the letters ASCII are selected (from 65 to 90 - capital letters ASCII, and 97 to 122 are lowercase).
Regex approach: (?<!\S) lookbehind does not match if there are no spaces before [a-zA-Z]+ (or the beginning of the line), 1 or more ASCII letters, and negative lookhhead (?!\S) does not match if there is no space (or end of line) after the letters.
source share