How to split a string by new lines in Swift

后端 未结 9 2460
一个人的身影
一个人的身影 2020-11-29 06:59

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 p

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 07:56

    in Xcode 8.2, Swift 3.0.1:

    Use NSString method components(separatedBy:)

    let text = "line1\nline2"
    let array = text.components(separatedBy: CharacterSet.newlines)
    

    Or use String method enumerateLines, like Leo Dabus's answer

提交回复
热议问题