Reputation: 3
If I have a list of string like
var MyList = new List<string>
{
"substring1", "substring2", "substring3", "substring4", "substring5"
};
is there any efficient way to determine which elements of that list are contained in the following string
"substring1 is the substring2 document that was processed electronically"
In this case the result should be
var MySubList = new List<string>
{
"substring1", "substring2"
};
Upvotes: 0
Views: 62
Reputation: 22809
Text
by whitespacesvar words = Text.Split(" ").OrderBy(word => word).Distinct().ToList();
words
, one for the patterns
)List<string> matches = new();
int patternIdx = 0, wordIdx = 0;
while(patternIdx < patterns.Count && wordIdx < words.Count)
{
}
int comparison = string.Compare(patterns[patternIdx],words[wordIdx]);
switch(comparison)
{
case > 0: wordIdx++; break;
case < 0: patternIdx++; break;
default:
{
matches.Add(patterns[patternIdx]);
wordIdx++;
patternIdx++;
break;
}
}
Here I've used C# 9 new feature switch + pattern matching.
If you can't use C# 9 then a if ... else if .. else
block would be fine as well.
For the sake of completeness here is the whole code
var Text = "substring1 is the substring2 document that was processed electronically";
var words = Text.Split(" ").OrderBy(x => x).Distinct().ToList();
var patterns = new List<string> { "substring1", "substring2", "substring3", "substring4", "substring5" };
List<string> matches = new();
int patternIdx = 0, wordIdx = 0;
while(patternIdx < patterns.Count && wordIdx < words.Count)
{
int comparison = string.Compare(patterns[patternIdx], words[wordIdx]);
switch(comparison)
{
case > 0: wordIdx++; break;
case < 0: patternIdx++; break;
default:
{
matches.Add(patterns[patternIdx]);
wordIdx++;
patternIdx++;
break;
}
}
}
Upvotes: 0
Reputation: 117064
We can use LINQ Where
to query, for every substring, whether the large string Contains
the substring:
var MyList = new List<string>
{
"substring1", "substring2", "substring3", "substring4", "substring5"
};
var Text = "substring1 is the substring2 document that was processed electronically";
var output = MyList.Where(x => Text.Contains(x)).ToList();
Upvotes: 1