PLease, read my comment to the question.
You have to
Split[
^] string on spaces, for example:
string address = "Please write any Sentence";
string filter = "any";
int counter = address.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries).Count(x=>x==filter);
Console.WriteLine("{0}", counter);
[EDIT]
I forgot to mention about upper/lower letters! Note that comparison: (
"any"=="Any"
) returns
false
.
I'd strongly recommend to read this:
How to: Compare strings (C# Programming Guide) | Microsoft Docs[
^]
String.Compare Method (String, String, Boolean) (System)[
^]
Good luck!
[EDIT #2]
Due to OP's comments, here is a complete example:
void Main()
{
string path = @"D:\data.txt";
string find = "any";
string[] lines = File.ReadAllLines(path);
int cTotal = 0;
foreach(string line in lines)
{
cTotal += Counter(line, find);
}
Console.WriteLine("A '{0}' has been found {1} time", find, cTotal);
}
public static int Counter(string line, string findword)
{
int c= 0;
string[] words = line.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);
foreach(string word in words)
{
if (string.Compare(word, findword, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase)==0)
{
c++;
}
}
return c
}
More:
File.ReadAllLines Method (String) (System.IO)[
^]