Kotlin Academy · Lesson

Key String Functions: trim, split, replace, contains

Apply the most useful String extension functions in real scenarios.

Lesson 4 of 413 steps

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

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"

split by String or Regex

split splits a string into a List by a delimiter 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

Pass a limit to split to control the maximum number of parts returned.
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

replace substitutes all occurrences; replaceFirst replaces only the first match.
val s = "hello hello hello"
println(s.replace("hello", "hi"))           // hi hi hi
println(s.replaceFirst("hello", "hey"))     // hey hello hello

replace with Regex

Use Regex as the first argument to replace pattern-based matches.
val text = "I have 3 cats and 12 dogs"
val noNumbers = text.replace(Regex("\\d+"), "N")
println(noNumbers)  // I have N cats and N dogs

contains and startsWith / endsWith

Check if a string contains a substring or begins/ends with a specific prefix/suffix.
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

Most string functions accept an ignoreCase parameter.
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)) // true

lines() and joinToString()

lines() splits a string by newlines. joinToString converts a collection back to a string.
val text = "line1\nline2\nline3"
val lines = text.lines()
println(lines.size)  // 3
println(lines.joinToString(" | "))

padStart, padEnd, repeat

Pad strings for alignment and repeat them.
println("42".padStart(6, '0'))  // 000042
println("hi".padEnd(5))         // "hi   "
println("-".repeat(20))          // --------------------

toIntOrNull and Parsing

Convert strings to numbers safely using toIntOrNull(), toDoubleOrNull() — they return null on failure.
println("42".toIntOrNull())       // 42
println("abc".toIntOrNull())      // null
println("3.14".toDoubleOrNull())  // 3.14
val n = "abc".toIntOrNull() ?: 0  // safe default

Quick Check

Which function splits a string into a List in Kotlin?

Recap

Key String functions: trim, split, replace, contains, startsWith, endsWith, padStart, repeat, joinToString. These cover 90% of string manipulation tasks!
Free to start

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

  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