Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn String Type | Core Data Types
Kotlin Data Types

bookString 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

Main.kt

copy
1234567891011
package 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.

question mark

How do you insert a variable's value into a String in Kotlin?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

SectionΒ 1. ChapterΒ 4
some-alt