我编写解析日志文件的Swift应用程序。 日志文件字符串:
substr1 substr2 "substr 3" substr4
我需要获取数组:[substr1, substr2, substr 3, substr4]
但如果我使用类似的东西:
print(stringLine.components(separatedBy: " "))
我得到了:[substr1, substr2, "substr, 3", substr4]
。
如何接收阵列:[substr1, substr2, substr 3, substr4]
?
答案 0 :(得分:3)
其中一种可能的解决方案是使用map
:
let testSting = "substr1 substr2 \"substr3\" substr4"
let mappedString = testString.components(separatedBy: " ").map({$0.replacingOccurrences(of: "\"", with: "")})
print(mappedString) //["substr1", "substr2", "substr3", "substr4"]
此问题需要使用正则表达式,但提供了此示例。因此,为了解决您的问题,可以这样做:
var testStingArray = testSting.replacingOccurrences(of: "\"", with: "").components(separatedBy: " ")
var arr = [String]()
var step = 0
while step < testStingArray.count {
var current = testStingArray[step]
var next = step + 1
if next < testStingArray.count {
if testStingArray[next].characters.count == 1 {
current += " " + testStingArray[next]
testStingArray.remove(at: next)
}
}
arr.append(current)
step += 1
}
print(arr)//["substr1", "substr2", "substr 3", "substr4"]
答案 1 :(得分:1)
或者,您可以根据CharacterSet
拆分字符串,然后过滤掉空白事件:
let stringLine = "substr1 substr2 \"substr3\" substr4"
let array = stringLine.components(separatedBy: CharacterSet(charactersIn: "\" ")).filter { !$0.isEmpty }
print (array)
输出:["substr1", "substr2", "substr3", "substr4"]
但如果其中一个'substrings'中有某个"
,那么这将无法正常工作,那么该特定子字符串也将被拆分。
答案 2 :(得分:1)
你最好使用正则表达式:
let pattern = "([^\\s\"]+|\"[^\"]+\")"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let line = "substr1 substr2 \"substr 3\" substr4"
let arr = regex.matches(in: line, options: [], range: NSRange(0..<line.utf16.count))
.map{(line as NSString).substring(with: $0.rangeAt(1)).trimmingCharacters(in: CharacterSet(charactersIn: "\""))}
print(arr) //->["substr1", "substr2", "substr 3", "substr4"]
答案 3 :(得分:0)
或者,只需遍历字符并保持引用部分的状态:
0.0.0.0
注意:此代码并未考虑双引号//: Playground - noun: a place where people can play
import UIKit
extension String {
func parse() -> [String] {
let delimiter = Character(" ")
let quote = Character("\"")
var tokens = [String]()
var pending = ""
var isQuoted = false
for character in self.characters {
if character == quote {
isQuoted = !isQuoted
}
else if character == delimiter && !isQuoted {
tokens.append(pending)
pending = ""
}
else {
pending.append(character)
}
}
// Add final token
if !pending.isEmpty {
tokens.append(pending)
}
return tokens
}
}
print ("substr1 substr2 \"substr 3\" substr4".parse()) // ["substr1", "substr2", "substr 3", "substr4"]
print ("\"substr 1\" substr2 \"substr 3\" substr4".parse()) // ["substr 1", "substr2", "substr 3", "substr4"]
print ("a b c d".parse()) // ["a", "b", "c", "d"]
可能用于转义单引号。但我不知道你的情况是否有可能。
https://tburette.github.io/blog/2014/05/25/so-you-want-to-write-your-own-CSV-code/