Bird
Raised Fist0
Kotlinprogramming~15 mins

String methods (substring, split, trim) in Kotlin - Deep Dive

Choose your learning style10 modes available
Overview - String methods (substring, split, trim)
What is it?
String methods like substring, split, and trim are tools to work with text in Kotlin. Substring extracts parts of a string, split breaks a string into pieces based on a separator, and trim removes extra spaces from the start and end. These methods help you change and analyze text easily.
Why it matters
Without these methods, handling text would be slow and error-prone because you'd have to write complex code to cut, divide, or clean strings. These methods save time and reduce mistakes, making programs that work with text more reliable and easier to write.
Where it fits
Before learning these methods, you should know what strings are and how to create them in Kotlin. After mastering these, you can learn about regular expressions and more advanced text processing techniques.
Mental Model
Core Idea
String methods let you cut, split, and clean text easily to get exactly the parts you need.
Think of it like...
Imagine a loaf of bread: substring is like slicing a piece from the loaf, split is like breaking the loaf into smaller chunks, and trim is like wiping off crumbs from the edges before eating.
String: "  Hello, Kotlin World!  "

  ┌───────────────┐
  │   substring   │
  │  "Kotlin"    │
  └───────────────┘
         ↓
  ┌───────────────┐
  │     split     │
  │ ["Hello", "Kotlin", "World!"] │
  └───────────────┘
         ↓
  ┌───────────────┐
  │     trim      │
  │ "Hello, Kotlin World!" │
  └───────────────┘
Build-Up - 8 Steps
1
FoundationUnderstanding Kotlin Strings
🤔
Concept: Learn what strings are and how to create them in Kotlin.
In Kotlin, a string is a sequence of characters enclosed in double quotes. For example: val greeting = "Hello". Strings store text data and you can print or use them in your program.
Result
You can create and print strings like "Hello" or "Kotlin".
Knowing what a string is and how to create it is the base for using any string method.
2
FoundationBasic String Access and Length
🤔
Concept: Learn how to get the length of a string and access characters by position.
You can find the length of a string with .length, e.g., greeting.length gives 5. You can get a character by index using brackets: greeting[0] is 'H'. Indexing starts at 0.
Result
You can find string length and get characters like 'H' from "Hello".
Understanding string length and indexing is essential before extracting parts with substring.
3
IntermediateExtracting Parts with Substring
🤔Before reading on: do you think substring includes the character at the end index or stops before it? Commit to your answer.
Concept: Learn how substring extracts a part of a string between two positions.
The substring method takes a start index and an optional end index. It returns the characters from start up to but not including end. For example, "Hello".substring(1,4) returns "ell" because it takes characters at positions 1, 2, and 3.
Result
"Hello".substring(1,4) → "ell"
Knowing substring excludes the end index prevents off-by-one errors when slicing strings.
4
IntermediateSplitting Strings into Lists
🤔Before reading on: do you think split removes the separator or keeps it in the results? Commit to your answer.
Concept: Learn how split breaks a string into parts using a separator and returns a list.
The split method divides a string wherever it finds the separator and returns a list of parts. For example, "a,b,c".split(",") returns ["a", "b", "c"]. The separator is removed from the results.
Result
"a,b,c".split(",") → ["a", "b", "c"]
Understanding that split removes separators helps you plan how to process each part separately.
5
IntermediateRemoving Spaces with Trim
🤔
Concept: Learn how trim removes spaces from the start and end of a string.
The trim method deletes all whitespace characters (spaces, tabs, newlines) from the beginning and end of a string. For example, " Kotlin ".trim() returns "Kotlin" without the extra spaces.
Result
" Kotlin ".trim() → "Kotlin"
Knowing trim cleans up strings helps avoid bugs caused by hidden spaces in user input or data.
6
AdvancedUsing Substring with Ranges
🤔Before reading on: do you think Kotlin supports substring with range syntax like 1..4? Commit to your answer.
Concept: Learn how to use Kotlin's range syntax to get substrings more cleanly.
Kotlin allows using ranges to get substrings: "Hello".substring(1..3) returns "ell". This is a shorthand for substring with start and end indexes. The end index is inclusive in the range, so substring(1..3) includes character at index 3.
Result
"Hello".substring(1..3) → "ell"
Using ranges makes substring calls clearer and less error-prone by including the end index explicitly.
7
AdvancedSplitting with Multiple Separators
🤔
Concept: Learn how to split strings using multiple separators or regular expressions.
You can split a string by multiple characters using a regex. For example, "a,b;c".split(",", ";") splits by comma or semicolon, returning ["a", "b", "c"]. This helps when data uses different separators.
Result
"a,b;c".split(",", ";") → ["a", "b", "c"]
Knowing how to split by multiple separators helps handle messy or inconsistent text data.
8
ExpertTrim Variants and Unicode Spaces
🤔Before reading on: do you think trim removes all kinds of spaces including Unicode spaces? Commit to your answer.
Concept: Explore how trim works with different whitespace characters and its limitations.
The standard trim removes ASCII whitespace like spaces and tabs but may not remove all Unicode whitespace characters. Kotlin also offers trimMargin and trimIndent for special cases. Understanding which spaces are removed helps avoid bugs with invisible characters.
Result
Standard trim("\u2003text\u2003") may keep some Unicode spaces.
Knowing trim's limits with Unicode spaces prevents hidden bugs in internationalized text processing.
Under the Hood
Kotlin strings are immutable sequences of characters stored in memory. Methods like substring create new string objects referencing parts of the original data without changing it. Split uses internal loops to find separators and build lists of substrings. Trim scans from both ends to find the first and last non-whitespace characters and returns a new string slice.
Why designed this way?
Strings are immutable to avoid accidental changes and improve safety. Creating new strings instead of modifying originals prevents bugs. The substring method excludes the end index to match common programming conventions and avoid off-by-one errors. Split removes separators to give clean parts for easier processing. Trim focuses on ASCII whitespace for performance and simplicity, with special methods for other cases.
┌───────────────┐
│ Original String│
│ " Hello "    │
└──────┬────────┘
       │ substring(start, end)
       ▼
┌───────────────┐
│ New String    │
│ "Hell"      │
└──────┬────────┘
       │ split(separator)
       ▼
┌───────────────┐
│ List<String>  │
│ ["Hel", "lo"]│
└──────┬────────┘
       │ trim()
       ▼
┌───────────────┐
│ Trimmed String│
│ "Hello"     │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does substring include the character at the end index? Commit to yes or no.
Common Belief:Substring includes the character at the end index.
Tap to reveal reality
Reality:Substring excludes the character at the end index; it goes up to but not including it.
Why it matters:Assuming inclusion causes off-by-one errors, leading to wrong extracted text.
Quick: Does split keep the separator characters in the output? Commit to yes or no.
Common Belief:Split keeps the separators as part of the resulting strings.
Tap to reveal reality
Reality:Split removes the separators and returns only the parts between them.
Why it matters:Expecting separators in results can cause bugs when processing split parts.
Quick: Does trim remove all whitespace characters including Unicode spaces? Commit to yes or no.
Common Belief:Trim removes all kinds of spaces, including all Unicode whitespace.
Tap to reveal reality
Reality:Trim removes common ASCII whitespace but may not remove all Unicode whitespace characters.
Why it matters:Assuming full removal can cause hidden spaces to remain, breaking text comparisons.
Quick: Can you use substring with a range that includes the end index? Commit to yes or no.
Common Belief:Substring cannot use ranges or includes the end index in ranges.
Tap to reveal reality
Reality:Kotlin supports substring with ranges where the end index is inclusive.
Why it matters:Not knowing this leads to more complex code and missed cleaner syntax.
Expert Zone
1
Substring creates new string objects but may share underlying character arrays internally for performance.
2
Split can accept regular expressions, allowing complex splitting rules beyond simple separators.
3
Trim does not modify the original string but returns a new one, preserving immutability.
When NOT to use
Avoid substring and split for very large texts or performance-critical code where repeated slicing causes overhead; consider using StringBuilder or streaming parsers instead.
Production Patterns
In real systems, split is often used to parse CSV or log files, substring extracts fixed-format fields, and trim cleans user input before validation or storage.
Connections
Regular Expressions
Builds-on
Understanding split prepares you to use regex-based splitting for more powerful text processing.
Immutable Data Structures
Same pattern
Knowing strings are immutable helps understand why methods return new strings instead of changing originals.
Text Editing in Word Processors
Similar pattern
Operations like cutting (substring), splitting paragraphs, and trimming spaces in text editors mirror these string methods conceptually.
Common Pitfalls
#1Off-by-one error in substring end index.
Wrong approach:val part = "Hello".substring(1,5) // expects 'ello' but gets 'ello' correctly val wrongPart = "Hello".substring(1,6) // error: index out of bounds
Correct approach:val part = "Hello".substring(1,5) // 'ello', end index exclusive // Do not use end index beyond string length
Root cause:Misunderstanding that substring end index is exclusive and must be within string length.
#2Expecting split to keep separators in results.
Wrong approach:val parts = "a,b,c".split(",") println(parts) // expects ['a,', 'b,', 'c'] but gets ['a', 'b', 'c']
Correct approach:val parts = "a,b,c".split(",") println(parts) // ['a', 'b', 'c']
Root cause:Assuming split returns separators as part of output strings.
#3Using trim expecting to remove all invisible spaces including Unicode.
Wrong approach:val text = "\u2003Hello\u2003" val trimmed = text.trim() println(trimmed) // expects 'Hello' but gets '\u2003Hello\u2003'
Correct approach:val text = "\u2003Hello\u2003" val trimmed = text.trim { it.isWhitespace() } println(trimmed) // 'Hello'
Root cause:Not knowing standard trim only removes ASCII whitespace, not all Unicode spaces.
Key Takeaways
Substring extracts parts of a string using start and end indexes, excluding the end index by default.
Split breaks a string into a list of parts by removing the separator characters.
Trim removes whitespace from the start and end of a string but may not remove all Unicode spaces.
Kotlin strings are immutable, so these methods return new strings without changing the original.
Understanding these methods helps you manipulate text safely and efficiently in Kotlin programs.

Practice

(1/5)
1. What does the Kotlin trim() method do when called on a string?
easy
A. Converts the string to uppercase
B. Splits the string into parts based on spaces
C. Extracts a part of the string by index
D. Removes spaces from the start and end of the string

Solution

  1. Step 1: Understand the purpose of trim()

    The trim() method removes whitespace characters from the beginning and end of a string.
  2. Step 2: Compare with other string methods

    Unlike split() or substring(), trim() does not split or extract parts but cleans spaces around the string.
  3. Final Answer:

    Removes spaces from the start and end of the string -> Option D
  4. Quick Check:

    trim() removes outer spaces [OK]
Hint: trim() cleans spaces only at string edges [OK]
Common Mistakes:
  • Thinking trim() splits the string
  • Confusing trim() with substring()
  • Assuming trim() changes case
2. Which of the following is the correct Kotlin syntax to get a substring from index 2 to 5 (exclusive) of a string text?
easy
A. text.substring(2..5)
B. text.subString(2, 5)
C. text.substring(2, 5)
D. text.substring(2, 6)

Solution

  1. Step 1: Recall Kotlin substring syntax

    The correct method call is substring(startIndex, endIndex) where endIndex is exclusive.
  2. Step 2: Check each option

    text.substring(2..5) uses a range which is invalid for substring. text.subString(2, 5) has wrong capitalization. text.substring(2, 6) uses 6 as endIndex which extracts more characters than asked.
  3. Final Answer:

    text.substring(2, 5) -> Option C
  4. Quick Check:

    substring(start, end) uses exclusive end [OK]
Hint: Use substring(start, end) with end exclusive [OK]
Common Mistakes:
  • Using range syntax inside substring
  • Wrong method name capitalization
  • Confusing inclusive vs exclusive end index
3. What is the output of the following Kotlin code?
val data = " apple, banana , cherry "
val fruits = data.trim().split(",")
println(fruits[1].trim())
medium
A. ananab
B. banana
C. anana
D. banan

Solution

  1. Step 1: Apply trim() on the original string

    Calling trim() removes spaces at start and end, so string becomes "apple, banana , cherry".
  2. Step 2: Split by comma and trim the second element

    Splitting by "," gives ["apple", " banana ", " cherry"]. Then trimming the second element " banana " removes spaces, resulting in "banana".
  3. Final Answer:

    banana -> Option B
  4. Quick Check:

    trim() cleans spaces after split [OK]
Hint: Trim after split to remove spaces around parts [OK]
Common Mistakes:
  • Not trimming after split, leaving spaces
  • Assuming split trims automatically
  • Confusing index positions in list
4. Find the error in this Kotlin code snippet:
val sentence = "Hello World"
val part = sentence.substring(6, 5)
println(part)
medium
A. substring end index is less than start index causing error
B. substring method requires only one argument
C. substring indexes are inclusive, so 6 to 5 is valid
D. No error, output is "World"

Solution

  1. Step 1: Check substring parameters

    substring(startIndex, endIndex) requires startIndex ≤ endIndex. Here, startIndex=6 and endIndex=5, which is invalid.
  2. Step 2: Understand Kotlin substring behavior

    This causes an exception because the end index is less than the start index.
  3. Final Answer:

    substring end index is less than start index causing error -> Option A
  4. Quick Check:

    substring end must be ≥ start [OK]
Hint: End index must be >= start index in substring [OK]
Common Mistakes:
  • Assuming substring indexes are inclusive
  • Using end index less than start index
  • Thinking substring accepts one argument only
5. Given a string input = " red, green , blue , yellow ", write Kotlin code to create a list of trimmed color names without spaces. Which code snippet correctly achieves this?
hard
A. input.split(",").map { it.trim() }
B. input.trim().split(",")
C. input.split(",").trim()
D. input.trim().map { it.split(",") }

Solution

  1. Step 1: Split the string by commas

    Using split(",") breaks the string into parts including spaces.
  2. Step 2: Trim spaces from each part

    Using map { it.trim() } removes spaces from each color name, producing a clean list.
  3. Step 3: Evaluate other options

    input.trim().split(",") trims only the ends before splitting, so inner spaces remain. input.split(",").trim() tries to trim a list, which is invalid. input.trim().map { it.split(",") } trims the whole string then maps incorrectly.
  4. Final Answer:

    input.split(",").map { it.trim() } -> Option A
  5. Quick Check:

    Split then trim each part for clean list [OK]
Hint: Split first, then trim each piece with map [OK]
Common Mistakes:
  • Trimming before split only removes outer spaces
  • Trying to trim a list directly
  • Incorrect chaining of methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard