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. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Key String Functions: trim, split, replace, contains” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Key String Functions: trim, split, replace, contains”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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