Working 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
123456789101112131415161718package 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.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Großartig!
Completion Rate verbessert auf 6.25
Working with Strings
Swipe um das Menü anzuzeigen
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
123456789101112131415161718package 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.
Danke für Ihr Feedback!