请问怎么实现在一个字符串中查找另一个子串的算法(不用String类提供的方法) 请问怎么实现在一个字符串中查找另一个子串的算法(不用String类提供的方法)谢谢各位 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看String里的indexOf不就可以了。 public class t2 { public static void main(String[] args) { t2 t = new t2(); System.out.println(t.isContain("testa", "es")); } public int isContain(String source,String subStr) { //CHAR数组 char[] sc = source.toCharArray(); char[] sub = subStr.toCharArray(); //查找数组,返回索引值,未找到返回 -1 for(int i = 0; i < sc.length; i ++) { if(checkChar(sc,i,sub,0)) { return i; } } return -1; } public boolean checkChar(char[] c1,int c1Index,char[] c2,int c2Index) { //长度判断 if(c2Index >= c2.length) return true; if(c1Index == c1.length && c2Index < c2.length) return false; //规递判断字符 if(c1[c1Index] == c2[c2Index]) return checkChar(c1,c1Index+1,c2,c2Index+1); else return false; } } public class ZhengZe { public static boolean getString(String str1, String str2) { return str1.matches(".*" + str2 + ".*"); } public static void printInfo(boolean b) { if (b) { System.out.println("含有要查询的字符串!"); } else { System.out.println("没有找到要查询的字符串!"); } } public static void main(String[] args) { String str1 = "abcde"; String str2 = "bcd"; String str3 = "345"; boolean b1 = getString(str1, str2); printInfo(b1); boolean b2 = getString(str1, str3); printInfo(b2); }} (不用String类提供的方法你看看 indexOF的算法,自己抄一个不就行了。 哈哈! 怎么在本地触发鼠标事件? java程序问题?? 推荐几本java swing 好点的书籍 求一条sql语句 在Swing中点击了切换语言按钮后,如何使整个画面刷新? 一个排列组合的算法 有注释代码,请大家帮小妹的忙! 请问如何一天结束了,就清空数据库呢? 请问如何实现中文的拼音或笔划排序 请问:JB中不同的FRAME中的函数、值怎么传递呀? 请问怎么实现在一个字符串中查找另一个子串的算法(不用String类提供的方法) java正则表达式学习辅导书推荐
public class t2 { public static void main(String[] args) {
t2 t = new t2();
System.out.println(t.isContain("testa", "es"));
}
public int isContain(String source,String subStr) {
//CHAR数组
char[] sc = source.toCharArray();
char[] sub = subStr.toCharArray();
//查找数组,返回索引值,未找到返回 -1
for(int i = 0; i < sc.length; i ++) {
if(checkChar(sc,i,sub,0)) {
return i;
}
}
return -1;
}
public boolean checkChar(char[] c1,int c1Index,char[] c2,int c2Index) {
//长度判断
if(c2Index >= c2.length) return true;
if(c1Index == c1.length && c2Index < c2.length) return false;
//规递判断字符
if(c1[c1Index] == c2[c2Index])
return checkChar(c1,c1Index+1,c2,c2Index+1);
else
return false;
}
}
public class ZhengZe { public static boolean getString(String str1, String str2) { return str1.matches(".*" + str2 + ".*");
} public static void printInfo(boolean b) { if (b) {
System.out.println("含有要查询的字符串!");
} else {
System.out.println("没有找到要查询的字符串!");
}
} public static void main(String[] args) { String str1 = "abcde";
String str2 = "bcd";
String str3 = "345"; boolean b1 = getString(str1, str2);
printInfo(b1); boolean b2 = getString(str1, str3);
printInfo(b2);
}
}