String interpolation

Kotlin supports string interpolation, it is a little bit complex to do it in the Java language but it is not a problem for Kotlin. We do not require a lot of code to do this task as Kotlin supports it natively. It makes the code easier to read and understand. Let's create an example:

val bookName = "Spring 5.0"val phrase = "The name of the book is $bookName"

As we can see,  it is a piece of cake to interpolate strings in Kotlin. Goodbye String.format() with a lot of arguments. We can use $bookName to replace the bookName variable value. Also, we can access the functions present in objects, but for that, we need to put curly braces. Check the following code:

val springFiveOld = Book("Claudio E. de Oliveira","Spring 5.0 ...

Get Spring 5.0 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.