2015-11-05 117 views
0

我很難弄清如果一個較小的字符串是一個較大字符串的子字符串。 例如: 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 –

回答

1

讓我們通過它

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'(較小字符串中的第一個字母),如果根本找不到它,那麼它不是子字符串。如果你確實找到了,看看兩個字符串中的下一個字母是否匹配。重複。 –

1

我想象兩種方法可以做到這一點。第一個建立在你的方法之上。

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'循環。應該說有很多方法可以做到這一點。 –

1

另一種方法。基本上它和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; 

}

相關問題