Kotlin:
Extracting substrings

How to:

In Kotlin, use substring, take, and drop functions.

fun main() {
    val text = "Hello, Kotlin!"

    println(text.substring(7, 13)) // Prints "Kotlin"
    
    // From start
    println(text.take(5)) // Prints "Hello"

    // From end
    println(text.takeLast(6)) // Prints "Kotlin!"

    // Dropping chars
    println(text.drop(7)) // Prints "Kotlin!"
}

Deep Dive

In the early days of programming, handling strings was manual and error-prone. In Kotlin, it’s easier, safer, and less resource-intensive, thanks to built-in functions and String class features.

Alternatives to substring include using regular expressions with Regex or split to dice up strings—but these methods can be overkill for simple tasks.

Implementation-wise, remember that strings are immutable in Kotlin. So, when you extract a substring, you’re actually creating a new String object, not changing the original.

See Also