简体   繁体   English

如何在文本中找到特定单词的每个选项

[英]How to find every options of specific word in text

I am beginner and I am trying to every word, which contains "srv" word in text.我是初学者,我正在尝试每个单词,其中包含文本中的“srv”单词。 Here is example这是示例

Hello, this is test TEST-SRVCole

and I want print TEST-SRVCole我想打印TEST-SRVCole

How to do it the most effective way?怎么做才最有效?

This is what I have:这就是我所拥有的:

 var test = text.Where(x => x.ToString().ToLower().Contains("srv")).ToArray();
            foreach (var item in test)
            {
                Console.WriteLine(item);
            }

But it does not nothing.但它并没有什么。

This:这个:

var test = text.Where(x => x.ToString().ToLower().Contains("srv")).ToArray();

does nothing, because you literally iterate over characters .什么都不做,因为你确实迭代了characters

A simple starting point is (a) splitting the string to words, (b) analyzing each of them.一个简单的起点是(a)将字符串拆分为单词,(b)分析每个单词。 Let's assume a word boundary is just a space:假设单词边界只是一个空格:

var parts = text.Split(' ');
foreach (var item in parts.Where(part => part.ToLowerInvariant().Contains("srv")))
{
    Console.WriteLine(item);
}

In reality, the texts you will be processing will, most likely, have other characters as word boundaries, such as punctuation.实际上,您将要处理的文本很可能会有其他字符作为单词边界,例如标点符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM