You cannot do this directly with string.Replace, but you could use the string.IndexOf to search for your string until it doesn't find a match
int counter = 0;
int startIndex = -1;
string string_1 = "a a a";
while((startIndex = (string_1.IndexOf("a", startIndex + 1))) != -1)
counter++;
Console.WriteLine(counter);
If this becomes of frequent use then you could plan to create an extension method
public static class StringExtensions
{
public static int CountMatches(this string source, string searchText)
{
if(string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(searchText))
return 0;
int counter = 0;
int startIndex = -1;
while((startIndex = (source.IndexOf(searchText, startIndex + 1))) != -1)
counter++;
return counter;
}
}
and call it with
int count = string_1.CountMatches("a");
The advantages of IndexOf are in the fact that you don't need to create an array of strings (Split) or an array of objects (Regex.Matches). It is just a plain vanilla loop involving integers.