如何遍历一个字符串中是否存在另一个字符串 如:“I1I2I3”这个字符串,有个子字符串“I1I3”,这个用contains判断是不存在在那个字符串中的有什么方法可以判断I1I3是存在那个字符串中的。既只要I1I2I3这个字符串只要有I1I3就可以了, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把字串逐个比较呀: public static void main(String[] args) { System.out.println(charContains("I1I2I3", "I1I3")); } public static boolean charContains(String s, String sub) { if (s == null || sub == null) return false; for (int i = 0; i < sub.length(); i++) { boolean contains = false; char c = sub.charAt(i); for (int j = 0; j < s.length(); j++) { if (c == s.charAt(j)) { contains = true; break; } } if (!contains) return false; } return true; } 既只要I1I2I3这个字符串只要有I1I3就可以了,那 I1I2I3 只要有 I 可以不? 个人认为比较简单的做法:public static void main(String[] args) { String str="I1I2I3"; String str1=str.replaceAll("I1I3", ""); System.out.print((!str.equals(str1))? "含有此字符串!":"不含有此字符串!");} :“I1I2I3”这个字符串,有个子字符串“I1I3”???怎么可能啊? 难道是这个? public static void main(String[] args) { String str = "I11I4I31I3I5I3"; String regex = "I(?=[1|3]{1})"; Pattern pat= Pattern.compile(regex); Matcher mat = pat.matcher(str); if(mat.find()){ System.out.println(" It contant"); }else{ System.out.println("It dose not contant"); } } 想使用java来做一个简单的项目:就是建立一个可以通过网络访问的数据库 log4j日志输出有问题 TOMCAT6.0.18怎么动态加载 项目里的图片文件 Java的智士快来呀 sun.net.ftp.ftpClient中如何删除ftp服务端的文件? 问个很菜的问题,请高手指点一下 求一程序问题 怎么设置JFrame窗口,使窗口打开时默认最大化显示? 不好意思,问一个很菜的问题,请大家不吝赐教 重金请教:Eclipse和CVS结合使用,不能使用Incoming模式? 请问大师如何在java程序中播放音乐? Java 将JPanel上画的图片保存起来问题
System.out.println(charContains("I1I2I3", "I1I3"));
} public static boolean charContains(String s, String sub) {
if (s == null || sub == null)
return false; for (int i = 0; i < sub.length(); i++) {
boolean contains = false;
char c = sub.charAt(i);
for (int j = 0; j < s.length(); j++) {
if (c == s.charAt(j)) {
contains = true;
break;
}
}
if (!contains)
return false;
} return true;
}
public static void main(String[] args) {
String str="I1I2I3";
String str1=str.replaceAll("I1I3", "");
System.out.print((!str.equals(str1))? "含有此字符串!":"不含有此字符串!");}
String str = "I11I4I31I3I5I3";
String regex = "I(?=[1|3]{1})";
Pattern pat= Pattern.compile(regex);
Matcher mat = pat.matcher(str);
if(mat.find()){
System.out.println(" It contant");
}else{
System.out.println("It dose not contant");
} }