Confusion with StringBuilder in java

128 Views Asked by At

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

}
}
1

There are 1 best solutions below

0
On

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