I have created Search algorithm to search any string in the Text.I just want the count of matches returned and want to search by only one key. I have tested the performance by searching single character 'a' in file of size 2 mb and it takes around 7 seconds.Can you please suggest better algorithm or if i am missing anything here.
public int SearchText(string fromString,string searchText,bool isCaseSensitive)
{
int searchTextLength=searchText.Length;
if (!isCaseSensitive)
{
fromString = fromString.ToLower();
searchText = searchText.ToLower();
}
int matchCount=0;
while (fromString.Length > searchText.Length)
{
int firstMatchIndex=fromString.IndexOf(searchText[0]);
if (firstMatchIndex > -1)
{
if (searchText == fromString.Substring(firstMatchIndex, searchTextLength))
matchCount++;
fromString = fromString.Remove(0, firstMatchIndex + searchTextLength);
}
else
break;
}
return matchCount;
}