检查一个字符串包含字符串的列表,并保存匹配的人 [英] Check if a string contains a list of substrings and save the matching ones

查看:151
本文介绍了检查一个字符串包含字符串的列表,并保存匹配的人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:我有一个代表一个文本字符串

 字符串会将myText =文本来分析的话,酒吧,富; 

和话在里面搜索


$ B $名单b

 列表<串GT;字=新的List<串GT; {富,酒吧,XYZ}; 



我想知道最有效的方法,如果存在,得到的词列表文本中包含的,这样的事情:

 列表<串GT;比赛= myText.findWords(字)


解决方案

有没有在此查询,除非你专门的分析必须使用包含方法。所以,你可以试试这个:

 会将myText字符串=文本来分析的话,酒吧,富; 

名单,LT;字符串>字=新的List<串GT; {富,酒吧,XYZ};

变种结果= words.Where(ⅰ= GT; myText.Contains(ⅰ))了ToList();
//结果是:酒吧,富


This is my situation: I have a string representing a text

string myText = "Text to analyze for words, bar, foo";   

And a list of words to search for in it

List<string> words = new List<string> {"foo", "bar", "xyz"};

I'd want to know the most efficient method, if exists, to get the list of the words contained in the text, something like that:

List<string> matches = myText.findWords(words)

解决方案

There is no special analysis in this query except you have to use Contains method. So you may try this:

string myText = "Text to analyze for words, bar, foo";

List<string> words = new List<string> { "foo", "bar", "xyz" };

var result = words.Where(i => myText.Contains(i)).ToList();
//result: bar, foo

这篇关于检查一个字符串包含字符串的列表,并保存匹配的人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆