String Basics

Creating Strings

Example

A String is a sequence of characters in double quotes. Access a character with [index] (starting at 0) and get the length with .length.

fun main() {
    val app = "WhatsApp"
    println(app)
    println(app.length)
    println(app[0])
    println(app[app.length - 1])
    println(app.first() + " " + app.last())
}

Output

WhatsApp
8
W
p
W p

Strings are Immutable

Example

String functions never change the original; they return a new string.

fun main() {
    val city = "bengaluru"
    val upper = city.uppercase()
    println(city)    // unchanged
    println(upper)
}

Output

bengaluru
BENGALURU

Joining and Comparing

Example

Use + to join strings and == to compare their content (no .equals() needed as in Java).

fun main() {
    val first = "Ravi"
    val last = "Kumar"
    val full = first + " " + last
    println(full)

    println(full == "Ravi Kumar")
    println("kotlin".equals("KOTLIN", ignoreCase = true))
    println("apple" < "banana")    // alphabetical comparison
}

Output

Ravi Kumar
true
true
true

Note: In Java, == on strings compares references and is a classic bug. In Kotlin, == compares content. Use === only when you really mean "same object".