开发者

How to find if a string contains any items of an List of strings?

I have a string and a List of strings:

string motherString = "John Jake Timmy Martha Stewart";

and I want to find if that string contains any of the strings in a list ie:

var children = new List<string>{"John", "Mike"开发者_JS百科, "Frank"};

So I want to find out if motherString contains one of the items from children ie. 'John'

What would be the best way of going about this?


The simplest code I could come up with would be:

var hasAny = children.Any(motherString.Contains);

If you expect each of the words to be seperated by a space then you could use this:

var hasAny = motherString.Split(new[] { ' ' }).Any(children.Contains);

If the words in motherString could be seperated by other characters, you could add them like this:

motherString.Split(new[] { ' ', ',', ':' })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜