What does trimIndent() do to a multi-line raw string, and how does it decide how much to remove?
answer
- Removes the common minimal leading whitespace
- Blank lines ignored when computing indent
- Drops leading/trailing blank line
- Relative (nested) indent preserved
- Runtime String extension, not a keyword
basics
~10 strimIndent() finds the common leading whitespace shared by all non-blank lines and removes that much from every line, so indented source code produces clean, left-aligned text.
solid answer
~50 s`trimIndent()` is a `String` extension that normalizes the indentation of a multi-line string. It splits on line breaks, computes the **minimal common leading whitespace** across all lines that contain non-whitespace characters, and strips exactly that prefix from every line. Blank lines are ignored when computing the common indent and are emitted as empty. It also removes the first line if it is blank and the last line if it is blank, so the typical pattern where you open `"""` and immediately break the line yields clean output. This lets you indent a raw string to match surrounding code while the runtime value stays left-aligned. It does NOT use a margin marker; if lines are indented inconsistently, only the shared minimum is removed, preserving relative indentation. It's evaluated at runtime (a normal function call), though the compiler can constant-fold it.
code
kotlin · 9 linesval sql = """
SELECT id, name
FROM users
WHERE active = true
""".trimIndent()
println(sql)
// SELECT id, name
// FROM users
// WHERE active = true <- extra 2 spaces preserved (relative indent)go deeper
Knows it cleans up indentation so the printed string is left-aligned.
Explains the common-minimal-indent rule, blank-line handling, and that relative indent is preserved.
Contrasts it with trimMargin, notes runtime-call semantics and constant folding, and flags tab/space pitfalls.
Considers performance of repeated calls on hot paths, readability conventions, and when to externalize large text to resources instead.
## The problem it solves When you embed a multi-line raw string inside indented code, every line picks up the source indentation: ```kotlin fun build(): String { return """ line A line B """ } ``` Without cleanup, each line would start with 8 spaces. `trimIndent()` fixes that. ## The algorithm `trimIndent()` is an extension function on `String` (in `kotlin.text`). It: 1. **Splits** the string into lines on line terminators. 2. **Computes the common minimal indent**: among all lines that are **not blank** (contain at least one non-whitespace char), it finds the smallest number of leading whitespace characters. Blank lines are excluded from this calculation. 3. **Removes that many leading characters** from every line. 4. **Drops a leading blank first line and a trailing blank last line**, which are the artifacts of putting `"""` on its own. So the example above produces exactly: ``` line A line B ``` with no leading spaces and no surrounding blank lines. ## Relative indentation is preserved Only the *common minimum* is stripped, so nested structure survives: ```kotlin val json = """ { "a": 1 } """.trimIndent() // { // "a": 1 // } ``` The inner line keeps its extra 4 spaces because the common indent was the outer 4. ## Key facts - It is a **runtime** function call (an ordinary extension), not a compiler keyword — though `trimIndent` on a constant literal is recognized and can be optimized. - It uses **whitespace counting**, not a sentinel character — contrast with `trimMargin()`. - Works on any `String`, not only raw strings, but it's most useful with `"""..."""`. - If indentation is mixed in a way you didn't intend (e.g., tabs vs spaces), results can surprise you because it compares character prefixes. ## When to prefer trimMargin instead Use `trimMargin()` when you want explicit control via a `|` marker, especially if some lines must intentionally begin with leading spaces that you don't want measured into a 'common minimum'.
- What happens to blank lines during trimIndent()?They are ignored when computing the common indent and are emitted as empty lines. A leading/trailing fully-blank line is removed entirely.
- Is trimIndent() a compiler feature or a library function?It's a regular `String` extension in the standard library, called at runtime, though the compiler may constant-fold it for literal inputs.
saying these in an interview costs you the question
- Saying it removes ALL leading whitespace from every line
- Claiming it needs a margin marker like |
- Thinking it strips trailing whitespace on each line
- Believing it destroys nested/relative indentation
- Calling it a language keyword rather than a function