Dates, Formatting, and Text
Split and Join Text
Splitting text creates pieces that can be joined in a new format.
Recombine words
split_join_text.swift
Replay: real traced execution (multi-file project)
let separator = "/"
let text = "swift trace replay"
let words = text.split(separator: " ")
let message = words.joined(separator: separator)
print(message)
let separator = "-"
let text = "swift trace replay"
let words = text.split(separator: " ")
let message = words.joined(separator: separator)
print(message)
let separator = "|"
let text = "swift trace replay"
let words = text.split(separator: " ")
let message = words.joined(separator: separator)
print(message)
separator ← /, text ← swift trace replay, words ← ["swift", "trace", "replay"]
1let separator→ / = "/" //@separator="-", "|"2let text→ swift trace replay = "swift trace replay"3let words→ ["swift", "trace", "replay"] = textswift trace replay.split(separator: " ")4let message→ swift/trace/replay = words["swift", "trace", "replay"].joined(separator: separator/)56print(messageswift/trace/replay)outputswift/trace/replay
separator ← -, text ← swift trace replay, words ← ["swift", "trace", "replay"]
1let separator→ - = "-"2let text→ swift trace replay = "swift trace replay"3let words→ ["swift", "trace", "replay"] = textswift trace replay.split(separator: " ")4let message→ swift-trace-replay = words["swift", "trace", "replay"].joined(separator: separator-)56print(messageswift-trace-replay)outputswift-trace-replay
separator ← |, text ← swift trace replay, words ← ["swift", "trace", "replay"]
1let separator→ | = "|"2let text→ swift trace replay = "swift trace replay"3let words→ ["swift", "trace", "replay"] = textswift trace replay.split(separator: " ")4let message→ swift|trace|replay = words["swift", "trace", "replay"].joined(separator: separator|)56print(messageswift|trace|replay)outputswift|trace|replay
split join
`split` separates a string into parts, and `joined` combines those parts with a chosen separator.