如何找到,如果一个字符串包含字符串列表的任何项目? [英] 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", "Frank"};
所以我想看看motherString包含儿童即项目之一。 约翰
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?
推荐答案
最简单的code我能想出是:
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);
如果在motherString的话可以由其他字符分隔,你可以像这样添加它们:
If the words in motherString could be seperated by other characters, you could add them like this:
motherString.Split(new[] { ' ', ',', ':' })
这篇关于如何找到,如果一个字符串包含字符串列表的任何项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文