The answer may differ slightly depending on where you have the text, in one place or in different ones, but the principle will be the same: create a variable, which will be our inserted number, increasing when we find a place to replace, break the text into lines, change and merge back. It should end up with something like this:
int num=0; String toReplace="abc"; String[] strings=text.split(" "); String result=""; for(String string:strings){ num++; if(result.euals("")){ result=string.replace(toReplace,num+""); }else{ result+=string.replace(toReplace,num+""); } } text=replace;