public class Main
{
{
int index = string.lastIndexOf(substring);
if (index == -1)
return string;
return string.substring(0, index) + replacement + string.substring(index+substring.length());
}
{
System.
out.
println(replaceLast
("abc",
"d",
"e")); System.
out.
println(replaceLast
("abca",
"a",
"e")); System.
out.
println(replaceLast
("abc",
"a",
"e")); System.
out.
println(replaceLast
("\"Position, fix, dial\"",
"\"",
"\\\"")); }
}
cHVibGljIGNsYXNzIE1haW4KewogICBzdGF0aWMgU3RyaW5nIHJlcGxhY2VMYXN0KFN0cmluZyBzdHJpbmcsIFN0cmluZyBzdWJzdHJpbmcsIFN0cmluZyByZXBsYWNlbWVudCkKICAgewogICAgICBpbnQgaW5kZXggPSBzdHJpbmcubGFzdEluZGV4T2Yoc3Vic3RyaW5nKTsKICAgICAgaWYgKGluZGV4ID09IC0xKQogICAgICAgICByZXR1cm4gc3RyaW5nOwogICAgICByZXR1cm4gc3RyaW5nLnN1YnN0cmluZygwLCBpbmRleCkgKyByZXBsYWNlbWVudCArIHN0cmluZy5zdWJzdHJpbmcoaW5kZXgrc3Vic3RyaW5nLmxlbmd0aCgpKTsKICAgfQogICAKICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIEV4Y2VwdGlvbgogICB7CiAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocmVwbGFjZUxhc3QoImFiYyIsICJkIiwgImUiKSk7CiAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocmVwbGFjZUxhc3QoImFiY2EiLCAiYSIsICJlIikpOwogICAgICAgU3lzdGVtLm91dC5wcmludGxuKHJlcGxhY2VMYXN0KCJhYmMiLCAiYSIsICJlIikpOwogICAgICAgU3lzdGVtLm91dC5wcmludGxuKHJlcGxhY2VMYXN0KCJcIlBvc2l0aW9uLCBmaXgsIGRpYWxcIiIsICJcIiIsICJcXFwiIikpOwogICB9Cn0=