- Hands-On Design Patterns with Kotlin
- Alexey Soshin
- 115字
- 2021-06-25 20:49:24
String interpolation
What if we would like to actually print those results?
First, as you may have already noticed, in one of the examples above, Kotlin provides a nifty println() standard function that wraps the bulkier System.out.println() from Java.
But, more importantly, as in many other modern languages, Kotlin supports string interpolation using the ${} syntax. Following on from the example before:
println("I would suggest: ${suggestGift(10)} ")
The preceding code would print:
I would suggest: a book
If you're interpolating a variable, and not a function, curly braces could be omitted:
val gift = suggestGift(100)
println("I would suggest: $gift ")
This would print the following output:
I would suggest: a guitar