日期:2014-05-20  浏览次数:20736 次

一个关于截取字符串的问题,请大家帮帮忙!
函数原型为String replace(String str,String old,String with),str为被截取的字符串,old为str的子串,with用来替代old子串:
要求是假如with含有'*'字符,则'*'可以代替str中的任意一个字符,如果这样的'*'有多个,并且与字符交替出现等等情况呢?
例:str = "csdnjavase" old = "d*j" with = "help"
输出"cshelpavase"
请大家给给点子!
------解决方案--------------------
没怎么看懂,这个能用不?

public static void main(String[] args) throws IOException {
System.out.println(replace("csdnjavase", "d*j", "help"));
System.out.println(replace("csdnjavase", "d***v", "help"));
}

private static String replace(String str, String old, String with){
old = old.replaceAll("\\*", ".");
return str.replaceAll(old, with);
}