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
}
}
substring
doesn't change the state of theStringBuilder
. It returns a String containing the requested substring. See the Javadoc for more details.I think what you were looking for is
StringBuilder#delete