Swift
String Conversion
Array
Programming
iOS Development

Convert Swift string to array

Master System Design with Codemia

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

Introduction

Converting a Swift string to an array is simple only when you define what the array should contain. Some tasks need characters, others need words, lines, bytes, or domain-specific tokens. Choosing the right conversion keeps Unicode behavior correct and avoids subtle bugs.

Choose the Target Array Type First

A Swift String is a collection of grapheme clusters, not plain bytes. That means Array(text) returns [Character], where each element is a user-perceived character.

swift
1import Foundation
2
3let text = "Café 🚀"
4let chars: [Character] = Array(text)
5print(chars)
6print(chars.count)

The output count may differ from byte count because combined Unicode sequences can represent one visible character.

Convert to Words Safely

If your goal is words, do not convert to characters and rebuild words manually. Use split for whitespace-delimited text.

swift
1import Foundation
2
3let sentence = "swift string into words"
4let words = sentence
5    .split(separator: " ")
6    .map(String.init)
7
8print(words)

For punctuation-heavy content, use CharacterSet tokenization so commas and periods do not become part of words.

swift
1import Foundation
2
3let raw = "Hello, world. Swift; arrays!"
4let tokens = raw
5    .components(separatedBy: CharacterSet.alphanumerics.inverted)
6    .filter { !$0.isEmpty }
7
8print(tokens)

Convert to Lines, CSV Fields, or Custom Delimiters

Many real tasks are not character arrays at all. You may need line arrays or delimited fields.

swift
1import Foundation
2
3let multiline = "first line\nsecond line\nthird line"
4let lines = multiline
5    .split(whereSeparator: \.isNewline)
6    .map(String.init)
7
8print(lines)

For comma-separated values, a basic split works for simple input.

swift
1import Foundation
2
3let csv = "apple,banana,cherry"
4let fields = csv.split(separator: ",").map(String.init)
5print(fields)

For production CSV with quoted commas, use a parser library or custom state machine rather than plain split.

Convert to Bytes Only When Needed

If you need wire-format data, convert to Data or [UInt8] using an explicit encoding.

swift
1import Foundation
2
3let text = "Codemia"
4if let data = text.data(using: .utf8) {
5    let bytes = [UInt8](data)
6    print(bytes)
7}

Always define encoding intentionally. Assuming UTF-8 is common, but make the choice explicit so future maintainers do not guess.

Performance Notes for Large Strings

String slicing in Swift can create Substring views that share storage with the original string. This is efficient, but converting every slice to String immediately can add allocation overhead.

Good pattern:

  • keep Substring during internal transformations
  • convert to String only at boundaries where stable ownership is needed

Example:

swift
1import Foundation
2
3let source = "a b c d e"
4let pieces: [Substring] = source.split(separator: " ")
5
6// Convert only when returning from API boundary.
7let stable = pieces.map(String.init)
8print(stable)

This approach helps when processing large logs or large imported text files.

Testing Conversion Logic

String conversion bugs often appear with non-ASCII input. Include tests for:

  • accented text
  • emoji
  • empty and whitespace-only input
  • multiple delimiters in sequence

A quick XCTest-style check:

swift
1import XCTest
2
3final class StringArrayTests: XCTestCase {
4    func testWordSplit() {
5        let s = "one two  three"
6        let words = s.split(separator: " ").map(String.init)
7        XCTAssertEqual(words, ["one", "two", "three"])
8    }
9}

These tests protect behavior during refactors and Swift version upgrades.

Common Pitfalls

  • Assuming one byte equals one visible character.
  • Using split for CSV data that contains quoted commas.
  • Converting every Substring to String too early in large pipelines.
  • Ignoring encoding when converting strings to byte arrays.
  • Testing only plain English input and missing Unicode regressions.

Summary

  • Decide the target array type before writing conversion code.
  • Use [Character] for visible characters, split for words and lines, and [UInt8] for byte-level work.
  • Handle Unicode intentionally to avoid broken indexing assumptions.
  • Keep Substring where possible for performance, convert at API boundaries.
  • Add tests with Unicode and delimiter edge cases to keep conversions reliable.

Course illustration
Course illustration

All Rights Reserved.