Question: In Java, can I iterate on the content of a StringBuilder and delete/insert/replace chars such that the loop will stay consistent? If so, best practice, shall I use for-loop, iterator, or traditional loop from zero to length()-1? E.g., 

```
StringBuilder b=new StringBuilder("12345");
for (int i=0; i< b.length(); i++) //traditional loop, iterator(which?),other?
  char c= b.chartAt(i);
  if(c == '1') b.deleteCharAt(i); // reduce the size,what is i pointing to now?
  if(c=='2') b.insert(i,"two"); //increase the size

```

}

Edit: say I have a big string and I need to do changes on it and I dont' want to produce copies every time. StringBuilder is a mutable string, how can I use it properly to do inplace changes? I know I can use replace / replaceall on the String itself, but this is not the point here.

Answer: 
You can do that, but you should change the value of `i` accordingly.

```
StringBuilder b=new StringBuilder("12345");
for (int i=0; i< b.length(); i++) {
      char c= b.chartAt(i);
      if(c == '1'){
          b.deleteCharAt(i);
          i--; // because you don't want to miss out the next char after deleting the present char
      }
      else if(c=='2'){
             b.insert(i,"two"); // I am not sure you want 2 or i
             i=i+2; // change this accordingly.
           }
}

```
If you want to iterate over a `StringBuilder` then you're probably doing something wrong, because it just's designed for the different purpose (e.g. dynamically string buildings as you've noticed from its name). If you want to get a new char-sequence by changing some of them use `String` if you don't have some specific requirements.