Reputation: 139
I'm trying to search a string to see if it contains any strings from a list,
var s = driver.FindElement(By.Id("list"));
var innerHtml = s.GetAttribute("innerHTML");
innerHtml
is the string I want to search for a list of strings provided by me, example
var list = new List<string> { "One", "Two", "Three" };
so if say innerHtml contains "One" output Match: One
Upvotes: 8
Views: 28333
Reputation: 109
For those who want to serach Arrray of chars in another list of strings
List WildCard = new() { "", "%", "?" }; List PlateNo = new() { "13eer", "rt4444", "45566" };
if (WildCard.Any(x => PlateNo.Any(y => y.Contains(x)))) Console.WriteLine("Plate has wildchar}");
Upvotes: 0
Reputation: 1791
You can do this in the following way:
int result = list.IndexOf(innerHTML);
It will return the index of the item with which there is a match, else if not found it would return -1.
If you want a string output, as mentioned in the question, you may do something like:
if (result != -1)
Console.WriteLine(list[result] + " matched.");
else
Console.WriteLine("No match found");
Another simple way to do this is:
string matchedElement = list.Find(x => x.Equals(innerHTML));
This would return the matched element if there is a match, otherwise it would return a null.
See docs for more details.
Upvotes: 6
Reputation: 726479
You can do it with LINQ by applying Contains
to innerHtml
for each of the items on the list:
var matches = list.Where(item => innerHtml.Contains(item)).ToList();
Variable matches
would contain a subset of strings from the list
which are matched inside innerHtml
.
Note: This approach does not match at word boundaries, which means that you would find a match of "One"
when innerHtml
contains "Onerous"
.
Upvotes: 3
Reputation: 1489
foreach(var str in list)
{
if (innerHtml.Contains(str))
{
// match found, do your stuff.
}
}
Upvotes: 1