if you don't want to go with RegEx, you can use something like this:
using System.Linq;
private char[] spaceandpunctuationdelimiters = new char[] {' ', '.', ',', '?', '!', ':', ';', '\t', '\r', '\n' };
private int GetTextMatchCount(bool ignorewhitespaceandpunctuation, bool ignorecase, string stringtosearch, string stringtofind)
{
if (string.IsNullOrEmpty(stringtosearch) || string.IsNullOrEmpty(stringtofind)) return 0;
if (ignorecase)
{
stringtosearch = stringtosearch.ToLowerInvariant();
stringtofind = stringtofind.ToLowerInvariant();
}
if (ignorewhitespaceandpunctuation)
{
string[] stringtonowhitespacestring;
stringtonowhitespacestring = stringtosearch.Split(spaceandpunctuationdelimiters, StringSplitOptions.RemoveEmptyEntries);
return stringtonowhitespacestring.Count(str => str == stringtofind);
}
return 0;
}
The code example here reflects my personal preferences: I'd rather write procedural code than use RegEx because I have not had much experience using RegEx; and, I like the idea of building small code "tools" that can be maintained/extended easily.
However, I recognize that RegEx is a very valuable tool to master, and, when the need arises, intend to study it in depth.
Your mileage may vary.