如何判断一个字符是否在某个字符串中?

dengshangkun 2007-11-21 06:30:14
怎么判断一个字符串是不是在另一个字符串中?例如a='asfad-12ds',b='-',怎么判断b是不在在a中?
...全文
117 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
捣鼓地瓜 2007-11-21
  • 打赏
  • 举报
回复
还是正则表达式比较好点 因为他只是关注结构模型,而不是具体的内容 o(∩_∩)o...
zt_soft 2007-11-21
  • 打赏
  • 举报
回复
import java.util.regex.*;

class Regex1{
 public static void main(String args[]) {
  String a= "asfad-12ds";
  String b="-";
  Pattern p=Pattern.compile(b);
  Matcher m=p.matcher(a);
  boolean result=m.find();
  System.out.println(result);
 }
}
正则表达式
luyuanzhiwindows 2007-11-21
  • 打赏
  • 举报
回复
高手呀
olivesoup 2007-11-21
  • 打赏
  • 举报
回复
上面那个写的不好,改进一下

private static boolean test() {
String test = "asfad-12ds";
return test.contains("-");
}
olivesoup 2007-11-21
  • 打赏
  • 举报
回复



private static boolean test() {
String test = "asfad-12ds";
if(test.contains("-")){
return true;
}
return false;
}

62,623

社区成员

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

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