如何使用charAt()和length()来编写是否是substring方法

时间:2015-06-14 08:24:05

标签: java string

我想写一个boolean方法subString()来判断字符串s1是否是s2的子字符串。

要求仅限于使用 charAt() length() String的方法。< / p>

E.g。

 Substring("abc","abcd")-> true

 Substring("at","cat")->true

 Substring("ac","abcd")->false

indexOf() 无法使用。

这是我到目前为止所得到的。

public class Q3 {
    public boolean subString(String str1, String str2) {
        String s1 = str1.toLowerCase();
        String s2 = str2.toLowerCase();
        for (i = 0; i < s1.length; i++) {
            for (j = 0; j < s2.length; j++) {
                if (s1.charAt(i) == s2.charAt(j))
                    return true;
            }
        }
        return false;
    }
}

测试类是:

public class Q3test {
    public static void main (String arg[]){
        Q3 Q3object = new Q3();
        System.out.println(Q3object.Substring("ac","abcd"));
    }
}

它失败subString("ac","abcd"),因为它返回true。

3 个答案:

答案 0 :(得分:4)

如果第一个字符匹配,则代码返回true。您需要将第一个String的所有字符包含在第二个String的子字符串中。

编辑:

我的原始代码错了。这是正确的代码:

        public static boolean subString(String str1, String str2)
        {
          String s1 = str1.toLowerCase();
          String s2 = str2.toLowerCase();
          for (int offset = 0; offset <= s2.length() - s1.length(); offset++) {
            int i = 0;
            for (; i < s1.length(); i++){
              if(s1.charAt(i) != s2.charAt(i+offset)) {
                break;
              }
            }
            // found a substring that starts at the current offset
            if (i == s1.length())
              return true;
          }
          return false;
        }

答案 1 :(得分:0)

字符串类的contains方法应该

有关示例,请参阅:http://www.tutorialspoint.com/java/lang/string_contains.htm

答案 2 :(得分:0)

您还应该检查字符是否有序,并且彼此相邻。您的代码仅检查是否存在字符。

public static boolean subString(String a, String b) {

    int checker = 0;
    String sub = a.toLowerCase();
    String supr = b.toLowerCase();

    // loops through all the characters of superstring
    for (int sp = 0; sp < supr.length(); sp++) {

        // finds the first character of the substring
        if (supr.charAt(sp) == sub.charAt(0)) {

            // loops through the characters of the substring
            for (int sb = 0, tmp = sp; sb < sub.length(); sb++, tmp++) {
                if (supr.charAt(tmp) == sub.charAt(sb))
                    checker++; // increments checker for every char match
            }
            // resets checker if not all characters for sub is equal to super based on their order
            if (checker != sub.length())
                checker = 0;
        }

        // match occurred if all characters of the substring is present in the superstring, 
        // in which they are in the same order, and all are existing consecutively
        if (checker == sub.length())
            return true;
    }
    return false;
}

假设我们有:

public static void main(String[] args) {
    System.out.println(subString("abc", "abcd"));
    System.out.println(subString("at", "cat"));
    System.out.println(subString("ac", "abcd"));
}

输出将是:

true
true
false