title: delete a substring from a string buffer using Java
body: <p>I want to remove a substring from a string buffer every time this substring occures, what I did so far is:</p>
  
  <pre><code>  public static void main(String[] args) {
  
          String text = "Alice Bob Alice Bob Alice Bob";
          String substr = "Alice";            
          StringBuffer strbuf = new StringBuffer(text);
          strbuf.indexOf(substr);
          strbuf.lastIndexOf(substr);
          while (strbuf != null) {
                  strbuf.delete(strbuf.indexOf(substr), strbuf.indexOf(substr) + substr.length());
                  System.out.println(strbuf.toString());
              }
  }
  </code></pre>
  
  <p>I want to remove every occurrence of "Alice". But it gives the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  </code></pre>
  
  <p>I think that the error in the while line.
  Any ideas?</p>