Is there replaceLast() in Java? I saw there is replaceFirst().
EDIT: If there is not in the SDK, what would be a good implementation?
No. You could do reverse / replaceFirst / reverse , but it's a bit expensive.
Find the index of the last occurrence of the substring. String myWord = "AAAAAasdas"; String toReplace = "AA"; String replacement = "BBB"; int start = myWord. lastIndexOf(toReplace);
To replace the first occurrence of a character in Java, use the replaceFirst() method.
It could (of course) be done with regex:
public class Test {      public static String replaceLast(String text, String regex, String replacement) {         return text.replaceFirst("(?s)"+regex+"(?!.*?"+regex+")", replacement);     }      public static void main(String[] args) {         System.out.println(replaceLast("foo AB bar AB done", "AB", "--"));     } }   although a bit cpu-cycle-hungry with the look-aheads, but that will only be an issue when working with very large strings (and many occurrences of the regex being searched for).
A short explanation (in case of the regex being AB):
(?s)     # enable dot-all option A        # match the character 'A' B        # match the character 'B' (?!      # start negative look ahead   .*?    #   match any character and repeat it zero or more times, reluctantly   A      #   match the character 'A'   B      #   match the character 'B' )        # end negative look ahead   Sorry to wake up an old post. But this is only for non-overlapping instances. For example
.replaceLast("aaabbb", "bb", "xx");returns"aaaxxb", not"aaabxx"
True, that could be fixed as follows:
public class Test {      public static String replaceLast(String text, String regex, String replacement) {         return text.replaceFirst("(?s)(.*)" + regex, "$1" + replacement);     }      public static void main(String[] args) {         System.out.println(replaceLast("aaabbb", "bb", "xx"));     } } 
                        If you don't need regex, here's a substring alternative.
public static String replaceLast(String string, String toReplace, String replacement) {     int pos = string.lastIndexOf(toReplace);     if (pos > -1) {         return string.substring(0, pos)              + replacement              + string.substring(pos + toReplace.length());     } else {         return string;     } }   Testcase:
public static void main(String[] args) throws Exception {     System.out.println(replaceLast("foobarfoobar", "foo", "bar")); // foobarbarbar     System.out.println(replaceLast("foobarbarbar", "foo", "bar")); // barbarbarbar     System.out.println(replaceLast("foobarfoobar", "faa", "bar")); // foobarfoobar } 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With