Update to cover either condition:
You can get a list of words from text
by adding spaces before capital letters and splitting on space. Then you can compare that result with list
using SequenceEqual()
.
Here's an example:
static void Main(string[] args)
{
List<string> list = new List<string> {"One", "Two", "Three", "Four", "Five" };
string text = "OneTwoThreeFourFive";
string withSpaces = AddSpacesToSentence(text, true);
List<string> list2 = withSpaces.Split(' ').ToList();
bool b = list.SequenceEqual(list2);
}
// Refer to: https://stackoverflow.com/a/272929/4551527
static string AddSpacesToSentence(string text, bool preserveAcronyms)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
StringBuilder newText = new StringBuilder(text.Length * 2);
newText.Append(text[0]);
for (int i = 1; i < text.Length; i++)
{
if (char.IsUpper(text[i]))
if ((text[i - 1] != ' ' && !char.IsUpper(text[i - 1])) ||
(preserveAcronyms && char.IsUpper(text[i - 1]) &&
i < text.Length - 1 && !char.IsUpper(text[i + 1])))
newText.Append(' ');
newText.Append(text[i]);
}
return newText.ToString();
}
Note that I got the implementation for AddSpacesToSentence from this answer: https://stackoverflow.com/a/272929/4551527
Another Update
By the way, if the order of the words in the lists is not important (in other words: "OneTwo" should match {"Two","One"}), then you can Sort()
both lists before doing the SequenceEquals()
Original (when I thought it was one-way comparison)
You could use All()
instead:
List<string> list = new List<string> {"One", "Two", "Three", "Four" };
string text = "OneTwoThreeFour";
list.All(s => text.Contains(s))
This will return true if all elements in the sequence satisfy the predicate (here, contains).
The snippet above returns true. If you add "Five" to list
(but leave text
the same) then it will return false.