Question: How can I find all indexes of a pattern in a string using c#? 

For example I want to find all `##` pattern indexes in a string like this `45##78$$#56$$JK01UU`

Answer: 
Edited the code to make it a cleaner function.

```
public IEnumerable<int> FindAllIndexes(string str, string pattern)
{
    int prevIndex = -pattern.Length; // so we start at index 0
    int index;
    while((index = str.IndexOf(pattern, prevIndex + pattern.Length)) != -1)
    {
        prevIndex = index;
        yield return index;
    }
}

string str = "45##78$$#56$$JK01UU";
string pattern = "##";

var indexes = FindAllIndexes(str, pattern);

```
Another one that tries to be efficient:

```
public IEnumerable<int> FindPatternIndexes(string input, string search)
{
    var sb = new StringBuilder(input);

    for (var i = 0; search.Length <= sb.Length; i++)
    {
        if (sb.ToString().StartsWith(search)) yield return i;
        sb.Remove(0,1);
    }
}

```