fork(17) download
  1. public class Main
  2. {
  3. static String replaceLast(String string, String substring, String replacement)
  4. {
  5. int index = string.lastIndexOf(substring);
  6. if (index == -1)
  7. return string;
  8. return string.substring(0, index) + replacement + string.substring(index+substring.length());
  9. }
  10.  
  11. public static void main(String[] args) throws Exception
  12. {
  13. System.out.println(replaceLast("abc", "d", "e"));
  14. System.out.println(replaceLast("abca", "a", "e"));
  15. System.out.println(replaceLast("abc", "a", "e"));
  16. System.out.println(replaceLast("\"Position, fix, dial\"", "\"", "\\\""));
  17. }
  18. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
abc
abce
ebc
"Position, fix, dial\"