I have a string arrary. Lets say as below
string[] lists = { "one", "two", "three"};
I have a string. Lets say as below
string title = "ONE_Page1";
I want to check, whether any of the array elements (lists) contains in my string starting (title).
So you want to check if any string in the list is a substring of your "main-string"?
bool contains = lists
.Any(s => title.IndexOf(s, StringComparison.InvariantCultureIgnoreCase) >= 0);
If you don't want to compare case-insensitively you could use String.Contains
which is more readable. But your sample data suggests that you want to compare case-insensitive.
There is always some hate for the Array.* functions, so here there is my variant :-)
string[] lists = { "one", "two", "three"};
string title = "ONE_Page1";
bool existsContain = Array.Exists(lists, x => title.IndexOf(x, StringComparison.InvariantCultureIgnoreCase) != -1);
bool existsStartsWith = Array.Exists(lists, x => title.StartsWith(x, StringComparison.InvariantCultureIgnoreCase));
It isn't clear if you want to match xxx_one_yyy
(the fist one does it) or only "starts with" (so only one_yyy
) (the second one does it)
Note that instead of StringComparison.InvariantCultureIgnoreCase you could use StringComparison.CurrentCultureIgnoreCase , depending on how you want your app to behave with internationalization.
You can use some simple LINQ:
string[] lists = { "one", "two", "three"};
string title = "ONE_Page1";
bool titleStartsWithAnElementInLists = lists.Any(
prefix => title.StartsWith(prefix, true, CultureInfo.InvariantCulture);
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.