How to split a string by new lines in Swift

后端 未结 9 2426
一个人的身影
一个人的身影 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:57

    let test1 = "Line1\n\rLine2\nLine3\rLine4"
    let t1 = test1.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
    let t2 = t1.filter{ $0 != "" }
    let t3 = t1.filter{ !$0.isEmpty }
    

提交回复
热议问题