java replaceLast()

Java中有replaceLast()吗? 我看到有replaceFirst()

编辑:如果没有在SDK中,什么是一个很好的实现?

它可以(当然)用正则expression式来完成:

 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", "--")); } } 

虽然有一点点cpu周期饥饿的前瞻,但这只会是一个问题,当处理非常大的string(和正在search的正则expression式的多次出现)。

一个简短的解释(在正则expression式为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 

编辑

抱歉唤醒一个旧post。 但是这只适用于不重叠的情况。 例如.replaceLast("aaabbb", "bb", "xx"); 返回"aaaxxb" ,而不是"aaabxx"

诚然,这可以解决如下:

 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")); } } 

如果你不需要正则expression式,这里是一个子string的替代。

 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(), string.length()); } else { return string; } } 

testing用例:

 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 } 

使用replaceAll并在您的模式之后添加一个美元符号:

 replaceAll("pattern$", replacement); 

看看你自己: String

或者实际上是你的问题“我如何实现replaceLast() ?”

让我尝试一个实现(这应该performance得非常像replaceFirst() ,所以它应该支持replacestring中的正则expression式和反向引用):

 public static String replaceLast(String input, String regex, String replacement) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); if (!matcher.find()) { return input; } int lastMatchStart=0; do { lastMatchStart=matcher.start(); } while (matcher.find()); matcher.find(lastMatchStart); StringBuffer sb = new StringBuffer(input.length()); matcher.appendReplacement(sb, replacement); matcher.appendTail(sb); return sb.toString(); } 

您可以将StringUtils.reverse()String.replaceFirst()

从apache使用StringUtils:

 org.apache.commons.lang.StringUtils.chomp(value, ignoreChar); 

没有。

你可以做reverse / replaceFirst / reverse ,但是有点贵。

如果检查的string是这样的话

 myString.endsWith(substringToReplace) == true 

你也可以做

 myString=myString.replaceFirst("(.*)"+myEnd+"$","$1"+replacement) 

它很慢,但工作:3

  import org.apache.commons.lang.StringUtils; public static String replaceLast(String str, String oldValue, String newValue) { str = StringUtils.reverse(str); str = str.replaceFirst(StringUtils.reverse(oldValue), StringUtils.reverse(newValue)); str = StringUtils.reverse(str); return str; } 

使用lookahead正则expression式分割你的干草堆,并replace数组中的最后一个元素,然后将它们重新组合在一起:D

 String haystack = "haystack haystack haystack"; String lookFor = "hay"; String replaceWith = "wood"; String[] matches = haystack.split("(?=" + lookFor + ")"); matches[matches.length - 1] = matches[matches.length - 1].replace(lookFor, replaceWith); String brandNew = StringUtils.join(matches); 

我也遇到过这样的问题,但是我用这个方法:

 public static String replaceLast2(String text,String regex,String replacement){ int i = text.length(); int j = regex.length(); if(i<j){ return text; } while (i>j&&!(text.substring(ij, i).equals(regex))) { i--; } if(i<=j&&!(text.substring(ij, i).equals(regex))){ return text; } StringBuilder sb = new StringBuilder(); sb.append(text.substring(0, ij)); sb.append(replacement); sb.append(text.substring(i)); return sb.toString(); } 

这真的很好。 只需要添加你想要replacestring的string,并在“他”放置你想要replace的子string的地方,并在你的新string中放置你想要的子string。

 import java.util.Scanner; public class FindSubStr { public static void main(String str[]) { Scanner on=new Scanner(System.in); String s=on.nextLine().toLowerCase(); String st1=s.substring(0, s.lastIndexOf("he")); String st2=s.substring(s.lastIndexOf("he")); String n=st2.replace("he","mt"); System.out.println(st1+n); } }