2016-10-15 60 views
0

我正在嘗試編寫一個程序,該程序需要一個字符串並從中刪除所有其他字符串的實例。例如:("Remove them all!", "em")將打印"Rove th all!"。但是,當我運行這個,它給了我java.lang.StringIndexOutOfBoundsException爲什麼我繼續接收java.lang.StringIndexOutOfBoundsException

public class LabFive { 

    public static String removeAll(String oldPhrase, String removal){ 
     String newPhrase = ""; 
     for(int i = 0; i <= oldPhrase.length(); i++){ 
      if(oldPhrase.substring(i, (removal.length() + i)) == removal) 
       newPhrase = newPhrase + oldPhrase.substring((removal.length() + 2 + i), oldPhrase.length());   
     } 
     return(newPhrase); 
    } 

    public static void main(String[] args) { 
     System.out.println(removeAll("AaAaAa", "a")); 
    } 
} 
+0

W¯¯ hy你不能用'replace(「em」,「」)'? –

+0

你爲什麼不看看給出的答案,或許[接受](http://stackoverflow.com/help/accepted-answer)最能幫助你的答案? –

回答

0

您的代碼似乎有幾個問題。首先,您不能使用==來檢查字符串是否相等,您必須使用String.equals()方法。 Read here。其次,您的for循環從0迭代到oldPhrase.length()(含),但嘗試將此長度值用於索引將導致發生異常。在java中,字符串具有從零開始的索引,因此索引從0開始並在oldPhrase.length()-1結束。

第三,你的邏輯看起來破了。方法的參數是beginIndexendIndex。所以:

newPhrase = newPhrase + oldPhrase.substring((removal.length() + 2 + i), oldPhrase.length()); 

串接oldPhrase的一部分,直到最後newPhrase會做你想要什麼。


這是我做到的。這個想法更簡單,也更清晰。我已添加評論以明確說明。

測試代碼住在Repl.it

public static String removeAll(String oldPhrase, String removal) { 

    // if removal is not found return the original string 
    if(oldPhrase.indexOf(removal) == -1) { 
     return oldPhrase; 
    } 

    int removalLength = removal.length(); // storing the length so as not to call .length() again and again 

    for(int i = 0; i < oldPhrase.length(); i++) { // note that <= will cause the exception too 
     int idxOfRemoval = oldPhrase.indexOf(removal); 

     if(idxOfRemoval == i) { // removal is found at the current index, i.e. at index i 
      // take substring from beginning to index of removal + 
      // substring from the end of removal to end of original string 
      oldPhrase = oldPhrase.substring(0, idxOfRemoval) + oldPhrase.substring(idxOfRemoval+removalLength); 
     } 
    } 
    return(oldPhrase); 
} 

public static void main(String[] args) { 
    System.out.println(removeAll("AaAaAa", "a")); 
} 

輸出:

AAA 
0

解釋java.lang.StringIndexOutOfBoundsException是在循環的最簡單方法:

for(int i = 0; i <= oldPhrase.length(); i++){...} 

因爲i打算等於oldPhrase.length()你有一個問題時間與得到的字符串:

oldPhrase.substring(i, (removal.length() + i)) 

所以你最終最終

oldPhrase.substring(oldPhrase.length(), (removal.length() + oldPhrase.length())) 

這是一個問題,因爲在一個字符串指數最高爲length - 1和你想在length訪問索引。

removeAll將遍歷您的字符串(像你一樣),只是檢查,對每個角色在i,如果removal從那裏開始,然後你想返回字符串將是蠻力方式

sub(0,i) + removeAll(the rest off your string starting at i+removal.length)

public static String removeAll(String oldPhrase,String removal) { 
    int rem = removal.length(); 
    int n = oldPhrase.length(); 
    // if length of oldPhrase is shorter than removal 
    // then there nothing you need to remove 
    if (n < rem) return oldPhrase; 

    // iterate over your string 
    for (int i = 0; i <= n - rem; i++) { 
     int j; 
     // check if there is a substring, removal, starting at i 
     for (j = 0; j < rem; j++) { 
      if (oldPhrase.charAt(i+j) != removal.charAt(j)) 
       break; 
     } 
     // if there is... 
     if (j == rem) { 
      // return stuff before substring you want to remove + 
      //  removeAll(the stuff after substring you want to remove) 
      return oldPhrase.substring(0,i) + removeAll(oldPhrase.substring(i+rem,n),removal); 
     } 
    } 
    return oldPhrase; 
} 

public static void main(String[] args) { 
    System.out.println(removeAll("AaAaAa", "a")); 
} 

輸出:

AAA

相關問題