Kotlin's string templates offer a simple and convenient way to interpolate expressions within strings. This can be useful when you need to insert dynamic content into a string, such as a user's name or the current date.
String templates start with a dollar sign $
followed by an expression in curly braces {}
. The expression can be any valid Kotlin code, which will be evaluated and its result inserted into the string.
For example, the following string template:
val name = "John"
println("Hello, $name!")
will print Hello, John!
.
You can also use string templates to insert the result of a function call:
fun getCurrentDate(): String {
// ...
}
println("Today is $getCurrentDate().")
If the expression is a complex expression, you can use the $
character to access its members:
data class User(val name: String, val age: Int)
val user = User("John", 30)
println("User $user.name is $user.age years old.")
This will print User John is 30 years old.
.
You can also use string templates to insert the result of a function call:
fun getCurrentDate(): String {
// ...
}
println("Today is $getCurrentDate().")
If the expression is a complex expression, you can use the $
character to access its members:
data class User(val name: String, val age: Int)
val user = User("John", 30)
println("User $user.name is $user.age years old.")
This will print User John is 30 years old.
.
You can also use the ${}
syntax to interpolate a complex expression:
data class User(val name: String, val age: Int)
val user = User("John", 30)
println("User ${user.name} is ${user.age} years old.")
This is equivalent to the previous example.
If you need to insert a literal $
or {
character in a string template, you can escape it with a backslash \
:
println("\${5 + 5}") // prints ${5 + 5}
println("\$5 + 5") // prints $5 + 5
If you need to insert a backslash character, you can escape it with another backslash:
println("\\") // prints \
By default, string templates preserve any leading or trailing whitespace in the interpolated expression. If you want to remove this whitespace, you can use the trimIndent()
or trimMargin()
methods:
val text = """
|Lorem ipsum dolor sit amet,
|consectetur adipiscing elit.
|""".trimMargin()
println(text) // prints Lorem ipsum dolor sit amet, consectetur adipiscing elit.
The trimIndent()
method removes all leading whitespace, while the trimMargin()
method removes all leading whitespace up to the first non-whitespace character on each line. In the example above, this character is |
.
If you interpolate a nullable expression in a string template, Kotlin will automatically insert a safe call ?.
to prevent a NullPointerException:
fun getUserName(): String? {
// ...
}
println("User name is ${getUserName()}.")
This will print User name is null.
if the getUserName()
function returns null
.
If you are sure that an expression is not null
, you can use the non-null assertion operator !!
to disable the safe call:
fun getUserName(): String? {
// ...
}
println("User name is ${getUserName()!!}.")
This will print User name is null.
if the getUserName()
function returns null
.
Kotlin's string templates offer a simple and convenient way to interpolate expressions within strings. This can be useful when you need to insert dynamic content into a string, such as a user's name or the current date.