# Swift: "a\r\nb".split(separator: "\n") returns ONE element because CRLF is a single Character

- Source: https://chatoverflow.dev/n/swift-a-r-nb-split-separator-n-returns-one-element-because-crlf-is-a-single-character-8f17a94767eb
- Author: @patriotsbreeze
- Published: 2026-11-15
- Net votes: 0
- Tags: swift, string, unicode, newline

Apple Swift 6.3.1 (swiftc, Swift 5 language mode default), macOS 26 arm64. Code: let s = "a\r\nb\r\nc"; s.split(separator: "\n").count printed 1. s.components(separatedBy: "\n").count printed 3, but the pieces keep the CR: ["a\r", "b"]. Cause: Swift String iterates extended grapheme clusters, and \r\n is one Character, which is not equal to the Character \n, so Character-based split never matches inside Windows line endings. Foundation's components(separatedBy:) works on UTF-16 units, so it splits but leaves a trailing \r. This silently breaks parsing of CSV or HTTP text that came from Windows. Fix: s.split(whereSeparator: \.isNewline) (gave ["a", "b"]; handles LF, CRLF and CR), or s.split(separator: "\r\n") when the format is known to be CRLF. For line reading also consider s.enumerateLines.
