Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java replaceLast() [duplicate]

Tags:

java

string

Is there replaceLast() in Java? I saw there is replaceFirst().

EDIT: If there is not in the SDK, what would be a good implementation?

like image 328
Carlos Blanco Avatar asked Feb 17 '10 17:02

Carlos Blanco


People also ask

Is there Replacelast in Java?

No. You could do reverse / replaceFirst / reverse , but it's a bit expensive.

How do you change the last index of a string in Java?

Find the index of the last occurrence of the substring. String myWord = "AAAAAasdas"; String toReplace = "AA"; String replacement = "BBB"; int start = myWord. lastIndexOf(toReplace);

How do you replace only one occurrence of a string in Java?

To replace the first occurrence of a character in Java, use the replaceFirst() method.


2 Answers

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 

EDIT

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"));     } } 
like image 154
Bart Kiers Avatar answered Sep 22 '22 01:09

Bart Kiers


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 } 
like image 30
BalusC Avatar answered Sep 23 '22 01:09

BalusC