如何查看字符串是否为子字符串。 Java

问题描述:

我很难弄清如果一个较小的字符串是一个较大字符串的子字符串。 例如: s1 =船 s2 =汽船 这是我的代码。我只能用循环和charAt。如何查看字符串是否为子字符串。 Java

public static boolean isSubstring(String s1, String s2){ 
    boolean substringcheck = false; 
    int correct = 0; 
    for(int i=0; i<s1.length(); i++){ 
     for(int j=0; j<s2.length(); j++){ 
      if(s1.charAt(i) == s2.charAt(j)){  
       correct++; 
       for(int n=0; n<s1.length(); n++){ 
        if(s1.charAt(n) == s2.charAt(j)){ 
         correct++; 
        }else{ 
         correct = 0; 
        } 
       } 
      } 
     } 
    } 
    if(correct == s1.length()){ 
     substringcheck = true; 
    }else{ 
     substringcheck = false; 
    } 
    return substringcheck; 

} 

}

我在if语句后放什么检查,如果在较小的字符串匹配的那些所有字符后,我们发现在大串的匹配困惑。

+1

我投票是题外话,因为它是一点点的努力 – nio

+0

一门功课,你需要另一个for循环的内部,如果,一个标志变量来决定,如果你发现一个关闭这个问题子字符串或者你可以使用一个break语句,那么你需要在两个循环中处理你的结束条件 – nio

+0

@Alexander这是不同的,因为我只能用于循环和charAt –

让我们通过它

s1 = boat 
s2 = steamboat 
i = 0 
j = 0 

//walking through the code: 

if(b == s) // nope, increment j (j now equals 1), begin next loop iteration 

if(b == t) // nope, repeat 

if(b == e) // nope, repeat until... 

if(b == b) // oh, this looks good! what do we need to do now? 

if(b == o) //j got incremented again, doh! 
+0

好吧我知道我需要停止for循环,一旦找到匹配。但是如何?我们还没有在课堂上学到这一点。 –

+0

我的猜测是将另一个for循环放在我的if语句中,看看其余的是否等于s1 –

+0

您正处在正确的轨道上。如果你先在纸上做出来,它会有所帮助。然后找出代码的外观。在较大的字符串中找到字母'b'(较小字符串中的第一个字母),如果根本找不到它,那么它不是子字符串。如果你确实找到了,看看两个字符串中的下一个字母是否匹配。重复。 –

我想象两种方法可以做到这一点。第一个建立在你的方法之上。

boolean containmentCheck(String big, String small) { 
    boolean contained; 
     try { 
      for (int i = 0; i < big.length(); i++) { 
       contained = big.charAt(i) == small.charAt(0); 
       if (contained) { 
        for (int j = 1; j < small.length(); j++) { 
         contained = big.charAt(i + j) == small.charAt(j); 
         if (!contained) { 
          i += j; 
          break; 
         } 
         if (j == small.length() - 1) 
          return contained; 
        } 
       } 
      } 
      if (big.length() == 0 && small.length() == 0) 
       contained = true; 
     } catch (IndexOutOfBoundsException e) { 
      contained = true; 
     } 
    return contained; 
} 

第二种是完全不同的,但我认为你会发现它更简单。

boolean containmentCheck(String big, String small) { 
    return big.contains(small); 
} 

这里要学的教训是:仔细阅读API

+0

谢谢,只有我可以使用.contains(); 。我们并没有学会休息;然而。 –

+0

@JeffTeague嗯,你现在已经学会了。 'break'会提前退出while,'for'和'do'循环。应该说有很多方法可以做到这一点。 –

另一种方法。基本上它和Ekemark一样。 你应该知道'继续'是什么。

boolean isStringSubString(String subStr, String mainStr) { 
boolean isSubString = false; 
//This is important. We need to loop only the difference of length times. 
int max = mainStr.length() - subStr.length(); 

outerLoop : for (int i = 0; i <= max; i++) {  
    int n = subStr.length(); 
    int j = i; 
    int k = 0; 
    while (n != 0) { 
    if (mainStr.charAt(j++) != subStr.charAt(k++)) { 
     continue outerLoop; 
    } 
    n--; 
    } 
    isSubString = true; 
    break outerLoop; 
} 
return isSubString; 

}