Strings in Kotlin are objects that represent sequences of characters. You can use them like in most modern languages, but Kotlin also gives you some powerful tools!
val name = "Kotlin" val greeting = "Hello, $name!" println(greeting)
Output: Hello, Kotlin!
$variable
is used for String Templates in Kotlin.
Kotlin allows you to write strings over multiple lines using triple quotes """
.
val message = """ Dear User, Welcome to Kotlin Tutorials! Regards, Team """.trimIndent() println(message)
Strings are objects with properties and methods:
val language = "Kotlin" println(language.length) // Number of characters println(language.uppercase()) // KOTLIN println(language.lowercase()) // kotlin println(language.reversed()) // niltoK
val a = "Hello" val b = "hello" println(a == b) // false (case-sensitive) println(a.equals(b, ignoreCase = true)) // true
You can loop through each character in a string using a for
loop:
val text = "Kotlin" for (char in text) { print("$char ") }
Output: K o t l i n
Use these quick challenges to practice:
reversed()
.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!