How to matches the string in a sentence with the array list string
Input - Machine Name is server1.domain.com
ArrayList of string - server1.domain.com, server2.domain.com, server3.domain.com
I want to return true if server1.domain.com
is found in the list that is matched with the part of the given input.
I have checked many examples and they all have provided as ArrayList.Contains(....)
, but that is not required and it is vice-versa.
if it is not an array, I can code like below and get my desired return value.
var serverName = "Machine Name is server1.domain.com";
serverName.Contains("server1.domain.com");
But, for an array / list, how can I get my desired return value.
Thank You
If you have the server names in an array, then you can loop over that array, and check whether one of the array elements is contained in the input string:
var input = "Machine Name is server1.domain.com";
var serverNames = new [] { "server1.domain.com", "server2.domain.com", "server3.domain.com" };
var found = false;
foreach (var serverName in serverNames)
{
if (input.Contains(serverName))
{
found = true;
break;
}
}
Alternatively you could use Linq instead of the foreach loop:
var found = serverNames.Any(serverName => input.Contains(serverName));
Note: if you want the comparison to be case-insensitive, then use
string.Contains(serverName, StringComparison.OrdinalIgnoreCase)
instead.
See here for an example: https://dotnetfiddle.net/CiVgqd