How does trimMargin() work, and when would you choose it over trimIndent()?
answer
- Default marker is the pipe |
- Strips leading whitespace up to and including the marker
- Custom marker via trimMargin("#")
- Preserves intentional spaces after the marker
- Trims leading/trailing blank line too
basics
~10 strimMargin() removes everything up to and including a marker character (a pipe | by default) at the start of each line. You mark where each line should begin, giving explicit control.
solid answer
~40 s`trimMargin()` is a `String` extension that, for each line, removes the leading whitespace up to and including a **margin prefix** — by default the pipe character `|` — and keeps the rest. You write each content line with `|` at the intended left edge: `|first`, `| indented`. Lines without the marker are left as-is (minus nothing). It also trims a leading/trailing blank line like `trimIndent()`. You can pass a custom marker via `trimMargin(marginPrefix = ">")`. Choose `trimMargin()` over `trimIndent()` when lines must intentionally start with spaces (so a 'common minimum' heuristic would misbehave), when you want the left edge to be explicit and self-documenting, or when blank-but-significant indentation matters. `trimIndent()` is simpler for plain blocks; `trimMargin()` trades a marker character for precision.
code
kotlin · 9 linesval banner = """
| *
| ***
|*****
""".trimMargin()
println(banner)
// *
// ***
// ***** <- leading spaces after | are intentionally preservedgo deeper
Knows you put a | at the start of each line and it gets removed.
Explains the algorithm, the custom-marker overload, and how it preserves intentional spaces.
Articulates the precise trade-off versus trimIndent and picks correctly for edge cases like aligned columns.
Sets team conventions (when each is allowed), considers maintainability and the cost of marker noise in large templates.
## What it does `trimMargin()` (extension on `String` in `kotlin.text`) normalizes a multi-line string using an explicit **margin prefix**. For each line it: 1. Skips leading whitespace, 2. If the next characters equal the margin prefix (default `"|"`), removes the whitespace **and** the prefix, keeping everything after it. 3. Lines that don't have the prefix after their leading whitespace are kept unchanged. It also removes a leading blank line and a trailing blank line, like `trimIndent()`. ```kotlin val text = """ |Title | bullet (2 real spaces kept) |Footer """.trimMargin() // Title // bullet (2 real spaces kept) // Footer ``` Everything before and including each `|` is dropped; whatever follows (including intentional spaces) is preserved exactly. ## Custom marker If your content itself contains `|`, pass another prefix: ```kotlin val t = """ #row1 #row2 """.trimMargin("#") ``` ## trimMargin vs trimIndent | Aspect | trimIndent() | trimMargin() | |---|---|---| | How left edge is found | Common minimal whitespace across non-blank lines | Explicit marker char per line | | Marker needed | No | Yes (`|` by default) | | Intentional leading spaces | Risk: counted into the common minimum | Safe: spaces after the marker are preserved | | Verbosity | Lower | Slightly higher (one char per line) | ## When to choose trimMargin() - Lines must **start with spaces by design** (ASCII art, aligned columns) and you don't want a heuristic to strip them. - You want the intended left boundary to be **visually explicit and robust** to future edits. - The block's natural indentation is inconsistent and a 'common minimum' would give the wrong result. Otherwise, `trimIndent()` is the lighter choice for ordinary indented blocks. ## Both are runtime functions Neither is a keyword; both are ordinary library calls, and both still let string templates expand because that already happened when the raw string was built.
- What if a line has no margin marker?trimMargin leaves that line unchanged (its leading whitespace is not removed because no prefix was matched).
- Why might trimIndent() be wrong for ASCII art?Lines that should start with spaces feed into the 'common minimum' calculation, so meaningful leading spaces could be stripped; trimMargin makes the edge explicit.
saying these in an interview costs you the question
- Saying the default marker is something other than |
- Claiming you cannot change the marker
- Thinking it removes trailing whitespace
- Believing spaces after the | are stripped
- Confusing it with trimIndent's common-minimum logic