求判断字符串A包含字符串B的方法,参与者都有分

kakugigo 2005-05-19 06:13:22
判断判断字符串A是否包含字符串B,如包含,则将B从A中删除

时间紧迫,请大家给的详细点,谢了
...全文
751 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jihanzhong 2005-05-20
  • 打赏
  • 举报
回复
处理包含多个B的最好方法
jihanzhong 2005-05-20
  • 打赏
  • 举报
回复
管他在不在,repalceAll


replaceAll
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

Pattern.compile(regex).matcher(str).replaceAll(repl)

Parameters:
regex - the regular expression to which this string is to be matched
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
kenping 2005-05-20
  • 打赏
  • 举报
回复
不知道 replaceAll 里面如果有跟正则表达式冲突的字符串要出问题。我以前遇到过,replaceAll里面的参数还是有一定的讲究的。
江南愚子 2005-05-20
  • 打赏
  • 举报
回复
同意replaceAll()
erwinrommel 2005-05-20
  • 打赏
  • 举报
回复
如果你没有有什么特殊的要求,indexof(subString)就可以啦,如果你要什么特别的要求,比如出去最多的子String,那么自己写算法
说好不能打脸 2005-05-19
  • 打赏
  • 举报
回复
public class Test2 {
public static void main(String[] src){
String father = new String("1234567890abcdefghijk");
String sun = new String("678905");

if(father.indexOf(sun) != -1){

father = father.replaceAll(sun,"");
System.out.println(father);
}
else{
System.out.println("不是子串!");
}
}
}

测试有效
kakugigo 2005-05-19
  • 打赏
  • 举报
回复
up
oyljerry 2005-05-19
  • 打赏
  • 举报
回复
嗯,先indexOf查找位置,然后subString截取就可以
syp1979 2005-05-19
  • 打赏
  • 举报
回复
indexOf取得位置.subString截取啊
kakugigo 2005-05-19
  • 打赏
  • 举报
回复
up

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧