Id try something like this

nr_N = mainstr.Find('N',0);
nr_M = mainstr.Find('M', nr_N+1) - nr_N - 1;
nr_R = mainstr.GetLength() - ( nr_N + nr_M + 2);
This is not tested and propably wrong too...