0Pricing
Kotlin Academy · Lesson

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

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!

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

  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