use regular expression:
example:
IEnumerable<string> allInputs = new[] {"70000", "89000", "89001", "90001"};
string commaSeparatedPostCodePatterns = @"89000 , 90\d\d\d";
if (string.IsNullOrWhiteSpace(commaSeparatedPostCodePatterns))
return;
string[] postCodePatterns = commaSeparatedPostCodePatterns
.Split(',')
.Select(p => p.Trim()) // remove white spaces
.ToArray();
var allMatches =
allInputs.Where(input => postCodePatterns.Any(pattern => Regex.IsMatch(input, pattern)));
foreach (var match in allMatches)
Console.WriteLine(match);
With such a problem, the initial requirements are very simple but quickly become more and more complex (due to internationalization, exceptions to the rule, some smart tester testing an unexpected limit-case like 90§$%
...). So regular expressions provide the best trade-off simplicity vs. extendability
The regex equivalent to *
is .*
. But in your case, you would rather need more restrictive patterns and use the \d
placeholder matching a single digit. Optional digit would be \d?
. Zero or more digits would be \d*
. One or more digits would be \d+
. Please look at the documentation for more details.