-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
  • 49,394
  • 10
  • 79
  • 128
Amit
  • 561
  • 3
  • 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

Dioxin
  • 13,888
  • 5
  • 38
  • 80
Lothar
  • 5,086
  • 1
  • 9
  • 26