Android
String Manipulation
Split Function
Java
Kotlin

Android Split string

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

String splitting is one of the most common parsing tasks in Android apps, from reading server fields to handling local configuration values. Bugs appear when delimiters are treated as plain text in one place and regex in another, or when whitespace and empty tokens are ignored. A reliable implementation starts with clear tokenization rules and test coverage for malformed input.

Core Sections

1. Understand split semantics in Kotlin and Java

Kotlin and Java both provide split utilities, but defaults differ depending on overloads and regex usage. In Kotlin, split can take chars, strings, or regex patterns. Prefer char or string delimiters when possible for better readability and fewer escaping mistakes.

kotlin
val raw = "id:42|name:maya|role:admin"
val fields = raw.split('|')
println(fields)

If delimiter has special regex meaning, avoid regex unless you actually need pattern behavior. For example, splitting by dot should use string delimiter or escaped regex explicitly.

2. Normalize whitespace and validate token count

Many parsing issues are not from splitting itself, but from inconsistent spacing and missing fields. Trim tokens and validate expected shape right away.

kotlin
1data class UserRow(val id: Int, val name: String, val role: String)
2
3fun parseUserRow(line: String): UserRow {
4    val parts = line.split('|').map { it.trim() }
5    require(parts.size == 3) { "Expected 3 fields but got ${parts.size}" }
6
7    val id = parts[0].removePrefix("id:").toInt()
8    val name = parts[1].removePrefix("name:")
9    val role = parts[2].removePrefix("role:")
10    return UserRow(id, name, role)
11}
12
13println(parseUserRow("id:42 | name: maya | role: admin"))

Failing fast with a clear message is better than allowing partially parsed values to flow through the app.

3. Keep trailing empty fields when protocol requires them

Some formats use trailing empty fields to represent optional values. If you drop them accidentally, field mapping shifts.

kotlin
1val input = "a,b,"
2val defaultSplit = input.split(',')
3val keepTrailing = input.split(',', ignoreCase = false, limit = 0)
4println(defaultSplit)
5println(keepTrailing)

In Kotlin, behavior depends on overload and limit. Verify with tests for your exact data format, especially when porting parsing code from Java.

4. Avoid using split for real CSV or quoted formats

split(',') is not a CSV parser. Quoted commas, escaped quotes, and multiline values require a proper parser. If input comes from spreadsheets or external systems, use a CSV library instead of ad hoc tokenization.

For lightweight custom protocols, keep grammar simple and documented:

  • one delimiter type
  • no quoting rules
  • explicit escaping policy

The simpler the grammar, the safer manual splitting becomes.

5. Performance considerations in Android apps

For short lines, split performance is rarely a bottleneck. For large files or high-frequency parsing in hot paths, reduce allocations:

  • parse once and cache typed objects
  • avoid repeated split in adapters and bind loops
  • use streaming parsers for very large payloads

Profile before optimizing. Most apps benefit more from correct parsing and error reporting than micro-optimizations.

6. Testing strategy

Build a compact test matrix:

  • normal line
  • extra whitespace
  • missing field
  • empty field
  • unexpected delimiter
  • invalid number conversion

These tests quickly reveal parser drift when format changes. Include at least one malformed example from real logs so the parser behavior matches production reality.

Common Pitfalls

  • Mixing regex and literal delimiters without realizing semantic differences.
  • Ignoring token count validation and accepting partial malformed input.
  • Dropping trailing empty fields when protocol relies on positional mapping.
  • Using simple split on quoted CSV-like data.
  • Parsing inside UI bind paths and creating unnecessary allocations.

Summary

  • Treat string splitting as part of a defined parsing contract, not a quick utility call.
  • Prefer literal delimiters and explicit trimming for predictable behavior.
  • Validate field counts and conversions immediately to fail fast.
  • Use dedicated parsers for complex formats such as CSV.
  • Keep parsing tests close to real production examples to prevent regressions.

Course illustration
Course illustration

All Rights Reserved.