String的基础问题
已知一条字符串A,B;写一个函数删除字符串A中出现的所以B。
如:A= "ABCDE "; B= "CD "
结果A= "ABE ";
------解决方案-------------------- String a = "ABCDE ";
String b = "CD ";
a = a.replaceAll(b, " ");
System.out.println(a);
------解决方案--------------------vdragon(紫龙) ( ) 信誉:100 Blog 加为好友 2007-04-10 12:08:39 得分: 0
String a = "ABCDE ";
String b = "CD ";
a = a.replaceAll(b, " ");
System.out.println(a);
==========================================================
这样写不是很好,可以测一下a= "ABCCDDE ";
结果将是 "ABCDE ",不是预期结果~~
可以如下改:
String a = "ABCCDDE ";
String b = "CD ";
while(a.indexOf(b) > = 0){
a = a.replaceAll(b, " ");
}
System.out.println(a);