Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

How do I stop using the plus sign to glue strings together?

If you're coming from Java or C#, you're probably used to the "string concatenation" nightmare—using + every few words to inject a variable. It's tedious and makes the code hard to read. In Kotlin, we use String Templates. You just put a $ sign before the variable name directly inside the double quotes.

val characterName = "Sylas"
val weapon = "Void Blade"

// The old, clunky way:
println("Hero " + characterName + " wields the " + weapon + ".")

// The Kotlin way:
println("Hero $characterName wields the $weapon.")

I can't stress enough how much cleaner this is. It reads like a sentence rather than a puzzle.

When do I actually need the curly braces?

You'll see $variable and ${expression}. The rule of thumb is simple: if you're just referencing a single variable, the braces are optional. But the moment you need to access a property, call a method, or do a calculation, you need those braces to tell Kotlin where the expression ends and the string resumes.

class Player(val name: String, val health: Int)

val hero = Player("Sylas", 85)

// This won't work as you expect:
println("Health is $hero.health") // Prints: Health is Player@5f2108 and then ".health"

// Use braces for properties:
println("Health is ${hero.health}") // Prints: Health is 85

I usually just use the braces if I'm in doubt, but for simple local variables, omitting them is the standard style.

Can I put actual logic or math inside the template?

Yes, you can put almost any valid Kotlin expression inside ${}. This is incredibly powerful for quick formatting, like calculating a total or transforming a string on the fly.

val strength = 15
val weaponBonus = 5

println("Total Attack Power: ${strength + weaponBonus}")
println("Character Name in Uppercase: ${characterName.uppercase()}")

A word of advice: don't go overboard here. If you find yourself writing a complex if/else block or a long mathematical formula inside a string template, stop. Calculate the value in a separate variable first, then inject that variable. Otherwise, your code becomes a mess to debug.

What if I actually want to print a dollar sign?

This is the one "gotcha." Since $ is a reserved character for templates, you can't just type it if you're building something like a price tag. The cleanest way to handle this is to use a template that evaluates to a dollar sign literal.

val goldPieces = 150
println("Your balance is ${'$'}$goldPieces") // Prints: Your balance is $150

It looks a bit weird at first—a template inside a template—but it's the most reliable way to escape the symbol without breaking your string.




📋 Practical Task

Build an RPG Equipment Tooltip Generator

Your task is to create a small program that generates a formatted "tooltip" for a piece of gear in a game. You need to use string templates to handle both simple variables and expressions.

  • Create three variables: itemName (String), baseDamage (Int), and critMultiplier (Double).
  • Create a final string called tooltip.
  • The tooltip string must use templates to display the item name and the base damage.
  • Inside the same template, calculate and display the "Maximum Burst" (which is baseDamage multiplied by critMultiplier).
  • Print the final tooltip to the console.

Example Output: Item: Dragon Slayer | Base: 50 | Max Burst: 125.0

Rating
0 0

There are no comments for now.

to be the first to leave a comment.