Browse - Programming Tips - Java: StringBuilder set content

Date: 2019mar8
Language: Java

Q.  Java: StringBuilder set content

A.  There isn't a method for this but there are two ways to do it.

// Make it and set some content StringBuilder sb = new StringBuilder("Hello "); sb.append("World"); // Change all content in the StringBuilder by new-ing sb = new StringBuilder("Totally new content"); // Another way, which is probably more efficient sb.replace(0, sb.length(), "Totally new content");