Home > Back-end >  Check if a string starts with a possible number of strings in C#?
Check if a string starts with a possible number of strings in C#?

Time:12-23

How can I most efficiently check to see if an input string starts with a string that belongs in a list of strings?

For example possiblePrefixes = "1234", "1235", "1236". If input = "1235av2425" should return true. If input = "1237352ko" should return false.

CodePudding user response:

you can use Any for this. the concept here is you need to check whether there is any item in the list which is the prefix for the given string.

List<string> list = new List<string>() { "1234", "1235", "1236" };
string input = "1237352ko";

var exisits = list.Any(x => input.StartsWith(x)); //returns false

when string input = "1235av2425"; it will return true

CodePudding user response:

An efficient datastructure for this type of search would be a prefix tree (aka "Trie").

For your example data such a tree might look something like this:

123
|-4
|-5
|-6

This could allow a lookup time that is independent of the number of prefixes you want to check against.

But as far as I know there are no builtin types for this, so you would either need to find a library, or implement it yourself.

CodePudding user response:

The solution using Any and StartsWith will be the best in most cases. Looking for an optimized solution will only be necessary if you have a long list of possible prefixes and/or a long list of texts to check against the same prefixes.

In that case, using a pre-compiled regular expression built once from the list of possible prefixes and then re-used for multiple checks might be a little faster.

// Build regular expression once
string[] possiblePrefixes = new string[] { "1234", "1235", "1236" };
var escaped = possiblePrefixes.Select(p => Regex.Escape(p)); 
string pattern = "^("   string.Join("|", escaped)   ").*";
Regex regEx = new Regex(pattern, RegexOptions.Compiled);

// Now use it multiple times
string input = "1235av2425";
bool result = regEx.IsMatch(input);
  • Related