Building from parts

suggest change

Programmatically generating a String is best accomplished with a StringBuffer. A StringBuffer doesn’t generate a new String object until toString() is called.

var sb = new StringBuffer();

sb.write("Use a StringBuffer ");
sb.writeAll(["for ", "efficient ", "string ", "creation "]);
sb.write("if you are ");
sb.write("building lots of strings");

var fullString = sb.toString();

print(fullString);
// Use a StringBuffer for efficient string creation if you are building lots of strings

sb.clear(); // all gone!

// The same writes, using a method cascade:
sb
  ..write("Use a StringBuffer ")
  ..writeAll(["for ", "efficient ", "string ", "creation "])
  ..write("if you are ")
  ..write("building lots of strings");

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents