Strings ยท SWIFT RECIPE

Split a string

Split text into fields and trim whitespace.

main.swift
import Foundation
let text = "Swift, Foundation, Concurrency"
let parts = text.split(separator: ",").map {
    String($0).trimmingCharacters(in: .whitespaces)
}
print(parts.joined(separator: " | "))

How it works

split returns Substring slices into the original text. Convert to String when storing the result independently for a long time.

EXPECTED OUTPUT
Swift | Foundation | Concurrency

Keep in mind

Use omittingEmptySubsequences: false when empty fields matter.

Read the Swift language reference