请问怎么实现在一个字符串中查找另一个子串的算法(不用String类提供的方法)
谢谢各位

解决方案 »

  1.   

    看看String里的indexOf不就可以了。
      

  2.   


    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;
    }

    }
      

  3.   


    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);
        }
    }
      

  4.   

    (不用String类提供的方法你看看 indexOF的算法,自己抄一个不就行了。 哈哈!