Key String Functions: trim, split, replace, contains
Apply the most useful String extension functions in real scenarios.
Key String Functions: trim, split, replace, contains is a free Kotlin Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Kotlin's Rich String API
trim, trimStart, trimEnd
val s = " hello "
println(s.trim()) // "hello"
println(s.trimStart()) // "hello "
println(s.trimEnd()) // " hello"
println("##hi##".trim('#')) // "hi"split by String or Regex
val csv = "a,b,c,d"
val parts = csv.split(",")
println(parts) // [a, b, c, d]
println(parts.size) // 4
val words = "one two three".split(Regex("\\s+"))split with Limit
val s = "a:b:c:d"
println(s.split(":", limit = 2)) // [a, b:c:d]
println(s.split(":", limit = 3)) // [a, b, c:d]replace and replaceFirst
val s = "hello hello hello"
println(s.replace("hello", "hi")) // hi hi hi
println(s.replaceFirst("hello", "hey")) // hey hello helloreplace with Regex
val text = "I have 3 cats and 12 dogs"
val noNumbers = text.replace(Regex("\\d+"), "N")
println(noNumbers) // I have N cats and N dogscontains and startsWith / endsWith
val s = "Hello, Kotlin!"
println(s.contains("Kotlin")) // true
println(s.startsWith("Hello")) // true
println(s.endsWith("!")) // true
println("kotlin" in s) // false (case-sensitive)Case-Insensitive Operations
val s = "Hello World"
println(s.contains("hello", ignoreCase = true)) // true
println(s.startsWith("HELLO", ignoreCase = true)) // true
println(s.equals("hello world", ignoreCase = true)) // truelines() and joinToString()
val text = "line1\nline2\nline3"
val lines = text.lines()
println(lines.size) // 3
println(lines.joinToString(" | "))padStart, padEnd, repeat
println("42".padStart(6, '0')) // 000042
println("hi".padEnd(5)) // "hi "
println("-".repeat(20)) // --------------------toIntOrNull and Parsing
println("42".toIntOrNull()) // 42
println("abc".toIntOrNull()) // null
println("3.14".toDoubleOrNull()) // 3.14
val n = "abc".toIntOrNull() ?: 0 // safe defaultQuick Check
Recap
Frequently asked questions
Is the “Key String Functions: trim, split, replace, contains” lesson free?
Yes — the full text of “Key String Functions: trim, split, replace, contains” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Key String Functions: trim, split, replace, contains”?
Apply the most useful String extension functions in real scenarios. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Key String Functions: trim, split, replace, contains” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String Basics: Literals, Escape Sequences & Length
- String Templates: $variable and ${expression}
- Raw Strings and Triple Quotes
- Key String Functions: trim, split, replace, contains