0
0
Kotlinprogramming~15 mins

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

Choose your learning style9 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.