String Type
Swipe to show menu
The String type in Kotlin is used to represent sequences of characters, such as words, sentences, or any other textual data. You use String variables to store and manipulate text. Declaring a String variable is straightforward: you specify the type String and assign it a value enclosed in double quotes. You can perform various operations on strings, including concatenation (joining strings together) and interpolation (inserting variable values directly into a string).
Main.kt
1234567891011package com.example fun main() { val firstName: String = "Alice" val lastName: String = "Smith" val fullName = firstName + " " + lastName val greeting = "Hello, $firstName!" println(fullName) println(greeting) }
In the code above, you see two common ways to combine and display text in Kotlin. Concatenation is performed using the + operator, as shown when creating the fullName variable by joining firstName, a space, and lastName. String interpolation uses the $ symbol to insert a variable's value directly into a string. In the greeting variable, $firstName is replaced with the value of the firstName variable, resulting in a personalized message. Both techniques allow you to build and work with dynamic text in your programs.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat