What is regex for replacing &f afrer same
I have string like this:
Hello &f are &f you &f here?
And I want to replace only last &f with "" (by last I mean third one, which is near "here". I tried something like this:
"Hello &f are &f you &f here?".replaceAll(".&f", "")
But it doesn't work, so could someone help me?
Can also be done with StringBuilder.reverse()
String str1 = "Hello &f are &f you &f here ?"; String temp = new StringBuilder(str1).reverse().toString().replaceFirst("f&", ""); String rslt = new StringBuilder(temp).reverse().toString(); System.out.println(rslt);
- Reverse the String using StringBuffer
- Replace first f& with the help of String.replaceFirst()
- Then again reverse that String.
Hello &f are &f you here ?
String s = "Hello &f are &f you &f here?"; s = s.substring(0,s.lastIndexOf("&f")) + s.substring(s.lastIndexOf("&f")+3); System.out.println("s = " + s); // prints: s = Hello &f are &f you here?
And replace with the backreference $1. (Or is it \1 in Java?)