Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Working with Strings | Kotlin Variables and Data Types
Practice
Projects
Quizzes & Challenges
Quiz
Challenges
/
Introduction to Kotlin

bookWorking with Strings

Strings are essential for working with text in any programming language. In Kotlin, you can join, or concatenate, strings using the + operator, but Kotlin also offers a more powerful feature: string templates. String templates let you embed variables and expressions directly inside a string using the $ symbol, making your code cleaner and easier to read. Here is how you can use both string concatenation and templates in Kotlin:

Main.kt

Main.kt

copy
123456789101112131415161718
package com.example fun main() { val firstName = "Ada" val lastName = "Lovelace" // String concatenation using + val fullName = firstName + " " + lastName println("Concatenated name: " + fullName) // String template with variables val greeting = "Hello, $firstName $lastName!" println(greeting) // String template with an expression val nameLength = "Your full name has ${fullName.length} characters." println(nameLength) }

This code demonstrates several ways to work with strings in Kotlin. The fullName variable uses the + operator to concatenate firstName, a space, and lastName. The greeting variable shows a string template, where $firstName and $lastName are placed directly inside the string, making the code concise and readable. The nameLength variable goes further by embedding an expression inside ${}—in this case, calculating the length of the full name. String templates let you easily include variable values or even run code within your strings.

question mark

Which statement about string templates in Kotlin is true?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookWorking with Strings

Scorri per mostrare il menu

Strings are essential for working with text in any programming language. In Kotlin, you can join, or concatenate, strings using the + operator, but Kotlin also offers a more powerful feature: string templates. String templates let you embed variables and expressions directly inside a string using the $ symbol, making your code cleaner and easier to read. Here is how you can use both string concatenation and templates in Kotlin:

Main.kt

Main.kt

copy
123456789101112131415161718
package com.example fun main() { val firstName = "Ada" val lastName = "Lovelace" // String concatenation using + val fullName = firstName + " " + lastName println("Concatenated name: " + fullName) // String template with variables val greeting = "Hello, $firstName $lastName!" println(greeting) // String template with an expression val nameLength = "Your full name has ${fullName.length} characters." println(nameLength) }

This code demonstrates several ways to work with strings in Kotlin. The fullName variable uses the + operator to concatenate firstName, a space, and lastName. The greeting variable shows a string template, where $firstName and $lastName are placed directly inside the string, making the code concise and readable. The nameLength variable goes further by embedding an expression inside ${}—in this case, calculating the length of the full name. String templates let you easily include variable values or even run code within your strings.

question mark

Which statement about string templates in Kotlin is true?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
some-alt