String output = "I\\AM\\So\\Cool"; while (true) { int pos = output.indexOf("\\"); // check to see if its not in there // eg: work has finished if (pos == -1) break; output = output.substring(pos+1) + " " + output.substring(0, pos); } // do the last word int pos = output.indexOf(" "); output = output.substring(pos+1) + " " + output.substring(0, pos);