如何分割在斯威夫特新行字符串 [英] How to split a string by new lines in Swift

查看:101
本文介绍了如何分割在斯威夫特新行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我从一个文本文件中得到了。

文本文件:

  1号线
2号线
3号线
...

我想将其转换为一个数组,每行一个数组元素。

  [1号线,2号线,3号线,...]

根据文件如何保存,字符串可以采取以下形式之一:


  • 字符串=1号线\\ n线2 \\ n线3 \\ n ...,其中 \\ n 是新行(换行)字符


  • 字符串=1号线\\ r \\ n线2 \\ r \\ n线3 \\ r \\ n ...,其中 \\ - [R 是回车符。


据我了解, \\ n 通常在今天的苹果/ Linux的使用,而 \\ r \\ n 在Windows中使用。

我如何分割在任何线路断线得到一个String数组没有任何空元素?

更新

有几种解决方案,以下工作。在这一点上,我没有任何令人信服的理由来选择一个作为比别人更正确。可能影响选择的一些因素可能是(1)如何雨燕是,(2)它是很长的字符串的速度有多快。可以通过upvoting其中的一个或多个和/或离开的注释提供反馈

看到这里我总结的答案


解决方案

您可以使用字符串方法 enumerateLines


  

枚举字符串中的所有行。


 让句子=1号线\\ n线2 \\ n线3 \\ n
VAR sentenceLines:[字符串] = []
sentence.enumerateLines {sentenceLines.append($ 0.line)}
打印(sentenceLines)//[1号线,2号线,3号线]

您可以创建一个只读属性计算扩展到返回线的排列如下:

 扩展字符串{
    VAR行:[字符串] {
        VAR结果:[字符串] = []
        enumerateLines {result.append($ 0.line)}
        返回结果
    }
}
让SENTENCE2 =4号线\\ n线5 \\ n线6 \\ n
让sentence2Lines = sentence2.lines打印(sentence2Lines)// [4号线,5号线,6号线]
让sentence3 =7号线\\ r \\ n线8 \\ r \\ n线9 \\ r \\ n
让sentence3Lines = sentence3.lines打印(sentence3Lines)//[7号线,8号线,9号线]

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

解决方案

You can use String method enumerateLines:

Enumerates all the lines in a string.

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

You can create a read only computed property extension to return an array of lines as follow:

extension String {
    var lines:[String] {
        var result:[String] = []
        enumerateLines{ result.append($0.line) }
        return result
    }
}


let sentence2 = "Line 4\nLine 5\nLine 6\n"
let sentence2Lines = sentence2.lines

print(sentence2Lines)    // ["Line 4", "Line 5", "Line 6"]


let sentence3 = "Line 7\r\nLine 8\r\nLine 9\r\n"
let sentence3Lines = sentence3.lines

print(sentence3Lines)  // "[Line 7, Line 8, Line 9]"

这篇关于如何分割在斯威夫特新行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆