String Interpolation
In the bounty-board project, the hero has a name. It would be better to use the name instead of always referring to “the hero.” You have the constant HERO_NAME; you just need to include its value in your strings. One way to accomplish this is string concatenation.
Update the welcome message in main to see how this works:
Listing 6.1 Using string concatenation (Main.kt
)
const val HERO_NAME = "Madrigal" var playerLevel = 5 fun main() {println("The hero announces her presence to the world.")println(HERO_NAME + " announces her presence to the world.") println(playerLevel) ... } ...
The +
operator can be used to concatenate, or join, two string values.
Here, you concatenate the string value of HERO_NAME ...
Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition 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.