Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string by new lines in Swift

I have a string that I got from a text file.

Text file:

Line 1 Line 2 Line 3 ... 

I want to convert it to an array, one array element per line.

[ "Line 1", "Line 2", "Line 3", ... ] 

Depending on how the file was saved, the string could take one of the following forms:

  • string = "Line 1\nLine 2\nLine 3\n..." where \n is the new line (line feed) character

  • string = "Line 1\r\nLine 2\r\nLine 3\r\n..." where \r is the carriage return character.

As I understand it, \n is commonly used in Apple/Linux today, while \r\n is used in Windows.

How do I split a string at any line break to get a String array without any empty elements?

Update

There are several solutions that work below. At this point I don't have any compelling reason to choose one as more correct than the others. Some factors that may influence choice could be (1) how "Swift" it is and (2) how fast it is for very long strings. You can provide feedback by upvoting one or more of them and/or leaving a comment.

See my summarized answer here

like image 538
Suragch Avatar asked Aug 15 '15 05:08

Suragch


People also ask

How do I split a string in Swift?

To split a string to an array in Swift by a character, use the String. split(separator:) function. However, this requires that the separator is a singular character, not a string.

How do you move to the next line in Swift?

By default Swift strings can't span more than one line. One simple way around this is to use the new line character \n , but that only works for strings that are displayed – if you're just trying to format your string nicely, you should use multi-line strings instead.

How do you wrap a string in Swift?

removeAtIndex(0) var newWord:String = removed + removeFirst println(removeFirst) println(arr3) println(newWord) arr3. insert(newWord, atIndex: 0) println(arr3) let res1 = join(" ", arr2) let res2 = join(" ", arr3) println(res1) println(res2) } override func didReceiveMemoryWarning() { super.


1 Answers

Swift 5.2 or later

You can split your String using the new Character property isNewline:

let sentence = "Line 1\nLine 2\nLine 3\n" let lines = sentence.split(whereSeparator: \.isNewline) print(lines)   // "["Line 1", "Line 2", "Line 3"]\n" 

You can also extend StringProtocol and create a lines instance property to break up the string lines into subsequences:

extension StringProtocol {     var lines: [SubSequence] { split(whereSeparator: \.isNewline) } } 

let sentence = "Line 1\nLine 2\r\nLine 3\n" for line in sentence.lines {     print(line) } let lines = sentence.lines  // ["Line 1", "Line 2", "Line 3"] 


Original Answer

You can use String method enumerateLines:

Enumerates all the lines in a string.

Swift 3 or later

let sentence = "Line 1\nLine 2\nLine 3\n" var lines: [String] = [] sentence.enumerateLines { line, _ in     lines.append(line) } print(lines)   // "["Line 1", "Line 2", "Line 3"]\n" 

extension String {     var lines: [String] {         var result: [String] = []         enumerateLines { line, _ in result.append(line) }         return result     } } 

let sentence2 = "Line 4\nLine 5\nLine 6\n" let sentence2Lines = sentence2.lines print(sentence2Lines)    // "["Line 4", "Line 5", "Line 6"]\n" let sentence3 = "Line 7\r\nLine 8\r\nLine 9\r\n" let sentence3Lines = sentence3.lines print(sentence3Lines)  // "["Line 7", "Line 8", "Line 9"]\n" 
like image 121
Leo Dabus Avatar answered Oct 09 '22 17:10

Leo Dabus