如何在 Swift 中按新行拆分字符串 [英] How to split a string by new lines in Swift

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

问题描述

我有一个从文本文件中获取的字符串.

I have a string that I got from a 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..." 其中 \n 是新行(换行)字符

  • 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..." 其中 \r 是回车符.

据我所知,\n 在今天的 Apple/Linux 中很常用,而 \r\n 在 Windows 中使用.

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?

下面有几种可行的解决方案.在这一点上,我没有任何令人信服的理由选择一个比其他的更正确.可能影响选择的一些因素可能是 (1) Swift"如何选择.它是和 (2) 对于很长的字符串它有多快.您可以通过对其中一个或多个投票和/或发表评论来提供反馈.

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.

推荐答案

Swift 5.2 或更高版本

您可以使用新的 Character 属性 isNewline:

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"


您还可以扩展 StringProtocol 并创建一个 lines 实例属性来将字符串行分解为子序列:


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"]



原答案

您可以使用字符串方法enumerateLines:

You can use String method enumerateLines:

枚举字符串中的所有行.

Enumerates all the lines in a string.

Swift 3 或更高版本

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"

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

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