Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

trying to match keyword in a string that in the form of a sentence

I have tried two different way to search this string and match it to the Appendixkeywords string. I have tried using Array.Exists and Array.IndexOf but both are not finding the Appendixkeywords. Any help would be great.

working sample

https://dotnetfiddle.net/QIXFDn

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I assume you want to check whether your source string contains any of the strings specified in your Appendixkeywords array. Additionally I assume you want to achieve a case insensitive comparison.

bool foundAnyAppendixWord = Appendixkeywords
    .Any(word => source
        .Contains(word, StringComparison.InvariantCultureIgnoreCase));

You could (and maybe even should) turn this easily into a reusable method

static bool ContainsAnySearchWord(string[] searchWords, string target)
{
    return searchWords.Any(word => target
         .Contains(word, StringComparison.InvariantCultureIgnoreCase));
}

// ...

bool foundAnyAppendixWord = ContainsAnySearchWord(Appendixkeywords, source);

.NET Framework (and older) versions:

static bool ContainsAnySearchWord(string[] searchWords, string target)
{
    return searchWords.Any(word => target.ToLower()
         .Contains(word.ToLower()));
}

Edit

You also have to add a using System.Linq; 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading