While your code example seems hard to read and could be more clear - I think this is what you are after:
string o=@"Reject count value";
int a = str.IndexOf(o);
if(a != -1)
{
a += o.Length;
string p = "Total:";
int b = str.IndexOf(p, a);
string best = str.Substring(a, b-a);
dataGridView1.Rows[n].Cells[5].Value = str;
}
Else
{
str = 0;
}