-1

Why is StringBuilder gives me an updated string when i use append but not when i use subString. Following is an example :-

class Hello{

 public void doSomething(){

StringBuilder sb = new StringBuilder("animals");
sb.append("s");
System.out.println("Original        = "+sb); // prints animals
sb.substring(sb.indexOf("a"), sb.indexOf("al"));
System.out.println(sb);// prints animals

}
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Amit
  • 633
  • 6
  • 13
  • 2
    Which part of documentation makes you think that `substring` should modify StringBuilder? – Pshemo Sep 03 '17 at 22:30

1 Answers1

3

substring doesn't change the state of the StringBuilder. It returns a String containing the requested substring. See the Javadoc for more details.

I think what you were looking for is StringBuilder#delete

Vince
  • 14,470
  • 7
  • 39
  • 84
Lothar
  • 5,323
  • 1
  • 11
  • 27