Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

StringBuilder

StringBuilder is the mutable partner to immutable String. Append runes, strings, or lines as you go, then take a snapshot with toString. Use it when a loop would otherwise call str::+ over and over (each concat allocates a new String).

You already saw a short builder example under Methods and the stream-shaped write members under String Streams. This page is the full API.

Park the builder on ts (or another aux stack) for a session of writes, then t:drop — same nesting style as other objects (Writing Clean Final Code).


Factories

KindNameEffectDescription
classnew( -- sb )empty builder
classnewWithPretty( indent:String -- sb )pretty mode: indentation string for nested structured output
StringBuilder.new^
"  " StringBuilder.newWithPretty^

Pretty builders are aimed at structured renderers (for example Json). For ordinary path joins, messages, and file assembly, plain new is enough.


Properties

NameTypeDescription
prettyBooltrue if created with newWithPretty (read-only)
indentStringStringindent unit used when pretty (read-only; empty for a plain builder)

Write and snapshot

KindNameEffectDescription
methodwriteRune( r:i -- )append one Unicode code point
methodwriteString( s:String -- )append as-is (no added newline)
methodwriteLine( s:String -- )append string, then \n
methodtoString( -- s:String )current contents as an immutable String
StringBuilder.new^ >ts
"hello" t:.writeString^
32 t:.writeRune^
"world" t:.writeLine^
t:.toString^ $              // hello world\n
t:drop

toString does not clear the builder; further writes keep appending. Build a fresh StringBuilder when you want a clean buffer.


Pretty / nesting helpers

Only meaningful when pretty is true (newWithPretty). Plain builders treat the pretty writes as ordinary writes (or no-ops where noted).

KindNameEffectDescription
methodwriteStringPretty( s:String -- )write current indent (if nested), then s
methodwriteSpacePretty( -- )write a space if pretty; otherwise nothing
methodwriteWhitespacePretty( -- )write \n if pretty, else a single space
methodnest( -- )increase indent level by one
methodunnest( -- )decrease indent level by one (stops at 0)
"  " StringBuilder.newWithPretty^ >ts
"{" t:.writeString^
t:.writeWhitespacePretty^
t:.nest^
"\"name\"" t:.writeStringPretty^
t:.writeSpacePretty^
":" t:.writeString^
t:.writeSpacePretty^
"\"Ada\"" t:.writeString^
t:.unnest^
t:.writeWhitespacePretty^
"}" t:.writeString^
t:.toString^ $ nl
t:drop

Typical pretty output:

{
  "name" : "Ada"
}

When to choose what

NeedPrefer
One or two concatenationsstr::+ / str::cat (String Operators)
Many appends in a loopStringBuilder
Character-at-a-time parse/buildRunes via foreach / TextReader, append with writeRune
Line-oriented file I/OTextReader / TextWriter (Streams)
Indented structured textnewWithPretty + nest/unnest

Strings remain Utf-8 end to end: every writeRune encodes a code point; toString yields a normal immutable String ready for str:: operators or $.


Using a StringBuilder as a TextWrite value

StringBuilder implements the TextWrite interface. This means you can supply a StringBuilder to contexts that accept a TextWrite, such as when you want to capture the stdout/stderr of a Pip (thread); or in Http bodies expecting a TextWrite Stream.