首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

一个关于截取字符串的有关问题,请大家帮帮忙

2012-12-23 
一个关于截取字符串的问题,请大家帮帮忙!函数原型为String replace(String str,String old,String with),s

一个关于截取字符串的问题,请大家帮帮忙!
函数原型为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);
}

热点排行