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");
foreach(var str in list)
{
if (innerHtml.Contains(str))
{
// match found, do your stuff.
}
}
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"
.
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.
For those who want to serach Arrray of chars in another list of strings
};<\/em> List PlateNo = new() { "13eer", "rt4<\/em> 444", "45566" };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.