我有这样的字符串:
Hello &f are &f you &f here?
我想用“”替换最后一个& f(最后我的意思是第三个,接近“这里”。我试过这样的事情:
"Hello &f are &f you &f here?".replaceAll(".&f", "")
但它不起作用,有人可以帮助我吗?
答案 0 :(得分:1)
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?
答案 1 :(得分:1)
答案 2 :(得分:1)
也可以使用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);
步骤:
f&
String.replaceFirst()
输出:
Hello &f are &f you here ?