English 中文(简体)
如何检查我的名单上是否有任何字句。 案文
原标题:How to check if any word in my List<string> contains in text
  • 时间:2011-02-02 12:18:31
  •  标签:
  • c#
  • linq

List<string> words = new List<string> {"word1", "word2", "word3"};

And i want to check using linq if my string contains ANY of these words; Smthng like:

var q = myText.ContainsAny(words);

And the second, if i have a list of sentences too:

List<string> sentences = new List<string> { "sentence1 word1" , "sentence2 word2" , "sentence3 word3"};

并且如果这些句子中有任何一句话,也包含其中的任何内容。

 var q = sentences.Where(s=>words.Any(s.text))....
最佳回答

如果大家需要检查一下子座,那么你可以使用一个简单的准则查询:

var q = words.Any(w => myText.Contains(w));
// returns true if myText == "This password1 is weak";

如果你想听一说的话,你可以定期表达:

  1. 1. 打击经常表达的言辞,即:

    // you may need to call ToArray if you re not on .NET 4
    var escapedWords = words.Select(w => @"" + Regex.Escape(w) + @"");
    // the following line builds a regex similar to: (word1)|(word2)|(word3)
    var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")");
    var q = pattern.IsMatch(myText);
    
  2. 定期将插手成字,并在文字收集上测试成员(如果你将言词输入而不是List,就会更快地做到这一点):

    var pattern = new Regex(@"W");
    var q = pattern.Split(myText).Any(w => words.Contains(w));
    

为了按照这一标准对判决进行过滤,你必须这样做。 在:

 // Given:
 // bool HasThoseWords(string sentence) { blah }
 var q = sentences.Where(HasThoseWords);

或者把它放在一个lam中:

 var q = sentences.Where(s => Regex.Split(myText, @"W").Any(w => words.Contains(w)));
问题回答
var q = words.Any(w => myText.Contains(w));

恢复所有包含1个或1个以上字的句子:

var t = sentences.Where(s => words.Any(w => s.Contains(w)));

            foreach (var sentence in t)
            {
                Console.WriteLine(sentence);
            }

虽然提出的大多数解决办法是实用的(所有电话Contains,这将为你提供所希望的解决办法),但如果名单和案文很大,你可能会有业绩问题。

从所述问题来看,我认为,你在空间或任何其他分歧之间说一句话。 因此,我建议你将<密码>myText分类为一份言词清单,并将其中每一个文本与你现在使用的文字清单进行比较。

当然,这更加复杂;你必须保证说几句正确——但是,如果大体(例如,文本档案)的话,可能会有一些业绩收益。

For your first condition

List<string> words = new List<string> { "word1", "word2", "word3" };
string test = "word1";
bool isFound = words.Contains(test);

For your second condition

bool isFound = sentences.Any(x => x.Split(new char[] {     }).Contains(test));

As an unrelated side-note

You are changing the question s scope after getting some answers and that is not a good way to ask questions. :)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签