2016-10-15 61 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

相关问题