CodeBlanks Logo Icon CodeBlanks

Summary - Strings

Combine templates, methods and split to write a business card.

Summary - Strings

In this summary you'll combine templates, methods and split to build a business card. In previous lessons you learned:

  • String templates with $ and ${}
  • methods like uppercase() and trim()
  • taking slices with substring(start, end)
  • dividing with split and swapping with replace

Using them together keeps real text tasks short. We'll do two mini tasks: print a cleaned name and split a comma-separated first and last name.

Practice Print the card name

Write a fun main(). Assign " alex " (lowercase, with spaces) to val name. Print the cleaned and capitalized name with println(name.trim().uppercase()).

Starter code

Main.kt
fun main() {
    // Clean and capitalize the name
}

Sample solution

Main.kt
fun main() {
    val name = "  alex  "
    println(name.trim().uppercase())
}

Expected output

ALEX

Practice Split first and last name

Write a fun main(). Assign "alex,smith" (comma-separated) to val full. Split it in two with val parts = full.split(","). Print parts[0] for the first name and parts[1] for the last name on separate lines.

Starter code

Main.kt
fun main() {
    // Split first and last name
}

Sample solution

Main.kt
fun main() {
    val full = "alex,smith"
    val parts = full.split(",")
    println(parts[0])
    println(parts[1])
}

Expected output

alex
smith

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 You want to clean and capitalize a name for a card. Which chain is right?

  • name.uppercase().trim()
  • name.split(",")
  • name.trim().uppercase() Correct answer
  • name.trim().lowercase()

Explanation: First trim cuts spaces, then uppercase makes capitals.

2 Complete the name template

Question
val card = "Name: ____"
Answer
val card = "Name: ${parts[0]}"

Explanation: ${parts[0]} embeds the first list item into the template.

All lessons in Beginner