Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele String Type | Core Data Types
Practice
Projects
Quizzes & Challenges
Visat
Challenges
/
Kotlin Data Types

bookString Type

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 4
some-alt