When working with strings in Kotlin — especially dynamic ones that embed variables — developers have long enjoyed the flexibility of string interpolation. But if you’ve ever had to include literal dollar signs in multiline strings, you’ve likely run into messy workarounds that hurt readability and feel clunky.
With the release of Kotlin 2.2.0-RC, JetBrains has introduced a powerful feature that’s both subtle and impactful: Multi-Dollar String Interpolation. This feature addresses the pain points around dollar sign usage in multiline strings, offering a clean, intuitive way to balance literal characters and string interpolation.
Note: Multi-dollar string interpolation, introduced as an experimental feature in Kotlin 2.1, is becoming stable in version 2.2.
In this post, we’ll explore what multi-dollar string interpolation is, why it matters, and how it simplifies real-world Kotlin code — especially when generating formats like JSON or dealing with currency values.
Quick Refresher: What Is String Interpolation?
String interpolation is the process of embedding variables or expressions directly into a string.
Here’s the basic syntax in Kotlin:
val name = "Amol"
println("Hello, $name") // Output: Hello, AmolYou can also interpolate expressions:
println("2 + 2 = ${2 + 2}") // Output: 2 + 2 = 4This works beautifully in single-line strings. But once you step into multiline strings, things get a little more complicated — especially when your string includes literal dollar signs.
The Problem: Dollar Signs in Multiline Strings
In single-line strings, Kotlin allows escaping special characters like so:
val price = "\$100"But in multiline strings — which use triple quotes ("""), Kotlin doesn’t support backslash escaping.
Let’s say you want to create a JSON template inside a multiline string that contains dollar signs for things like currency or schema declarations:
val json = """
{
"$schema": "https://example.com/schema",
"price": "$100"
}
"""Oops..! Kotlin will try to interpolate $schema and $100 as variables—likely leading to a compile-time error or unintended behavior.
The Workaround (Before Kotlin 2.2)
Previously, the only way to escape a dollar sign in multiline strings was to use:
"${'$'}"So your JSON would look like this:
val json = """
{
"${'$'}schema": "https://example.com/schema",
"price": "${'$'}100"
}
"""Technically correct — but let’s be honest, it’s ugly and hard to read, especially when you have multiple dollar signs in a single string.
The Solution: Multi-Dollar String Interpolation (Kotlin 2.2)
Kotlin 2.2 introduces multi-dollar string interpolation, which lets you define how many dollar signs are needed to trigger interpolation. This makes it easier to include literal dollar signs without messy escaping.
How It Works
Instead of starting a multiline string with """, you prefix it with $$""" or $$$"""—depending on how many dollar signs should be treated as interpolation triggers.
Example 1: $$ as Interpolation Trigger
val KClass<*>.jsonSchema: String
get() = $$"""
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"$dynamicAnchor": "meta",
"title": "$${simpleName ?: qualifiedName ?: "unknown"}",
"type": "object"
}
"""What’s Happening Here:
$$"""tells Kotlin that two dollar signs ($$) are required to interpolate.
So:
$= a literal dollar sign (no escaping needed).$$= still a literal (For example, let’s say “meta $$”. You’ll better understand it in the next example — just be patient.).$${...}= actual interpolation.
This makes your strings much cleaner and easier to maintain.
Example 2: $$$ as Interpolation Trigger
What if you want $ and $$ to always appear as literals, and only trigger interpolation with $$$?
val productName = "Amul"
val requestedData = $$$"""{
"currency": "$",
"enteredAmount": "72.45 $$",
"$$serviceField": "none",
"product": "$$$productName"
}
"""Output:
{
"currency": "$",
"enteredAmount": "72.45 $$",
"$$serviceField": "none",
"product": "Amul"
}Here,
$$$"""tells Kotlin: only$$$triggers interpolation.- So
$and$$stay literal in the string. $$$productNamegets interpolated to"Amul".
This is incredibly helpful in scenarios like:
- Generating JSON or XML.
- Embedding currency symbols.
- Working with configuration files.
- Using template engines or schema definitions.
Why This Matters: Cleaner, Safer Code
Multi-dollar interpolation isn’t just a syntactic sugar. It:
- Improves readability in strings with many dollar signs.
- Reduces errors caused by mistaken interpolations.
- Eliminates the awkward
${'$'}pattern. - Enhances maintainability when dealing with external formats like JSON, HTML, or YAML.
And importantly, it allows developers to express their intent more clearly: whether you’re writing a literal value or embedding a variable.
Conclusion
Multi-dollar string interpolation might seem like a small change, but for Kotlin developers who regularly deal with multiline strings, templates, or embedded formats, it’s a massive win. It cleans up your code, improves safety, and makes your intentions more explicit — all while keeping Kotlin’s expressive syntax intact.
If you’re working on data-heavy applications, web services, or even Android development with Kotlin DSLs or Compose, this is a feature you’ll definitely want to adopt.
