Home > Software design >  How to find every options of specific word in text
How to find every options of specific word in text

Time:08-31

I am beginner and I am trying to every word, which contains "srv" word in text. Here is example

Hello, this is test TEST-SRVCole

and I want print 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.

CodePudding user response:

This:

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

does nothing, because you literally iterate over characters.

A simple starting point is (a) splitting the string to words, (b) analyzing each of them. 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.

  • Related