????需求 ????
编程实现一个方法,能把一个字符串src的右侧出现的第一个olds子串替换为news,并把替换后的结果返回。
????代码 ????
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public class StringReplaceLast {
public static String replaceLast(String src, String olds, String news){ System.out.println(src); StringBuffer sbsrc = new StringBuffer(src); int lenolds = olds.length(); int tail = src.lastIndexOf(olds); sbsrc = sbsrc.replace(tail,tail+lenolds, news); src = sbsrc.toString(); return src; }
public static void main(String[] args) { System.out.println(replaceLast("goodJava,I love it, very good,Truly.", "good", "yes" )); }
}
|
????截图 ????
