Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Working with Strings | Kotlin Variables and Data Types
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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookWorking with Strings

Deslize para mostrar o 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
some-alt