0Pricing
Kotlin Academy · Lesson

Key String Functions: trim, split, replace, contains

Apply the most useful String extension functions in real scenarios.

Kotlin's Rich String API

Kotlin extends Java's String with dozens of extension functions. You'll use trim, split, replace, and contains constantly.

trim, trimStart, trimEnd

Remove whitespace (or specific characters) from the edges of a string.
val s = "   hello   "
println(s.trim())           // "hello"
println(s.trimStart())      // "hello   "
println(s.trimEnd())        // "   hello"
println("##hi##".trim('#')) // "hi"

All lessons in this course

  1. String Basics: Literals, Escape Sequences & Length
  2. String Templates: $variable and ${expression}
  3. Raw Strings and Triple Quotes
  4. Key String Functions: trim, split, replace, contains
← Back to Kotlin Academy