拆分字符串而不删除Swift

时间:2015-09-02 19:19:33

标签: swift

这可能是重复的。我在Swift找不到答案,所以我不确定。

componentsSeparatedByCharactersInSet删除分隔符。如果仅将一个可能的字符分开,则很容易将其添加回来。但是当你有一套时呢?

还有其他分割方法吗?

3 个答案:

答案 0 :(得分:3)

Swift 3和4版本

extension Collection {
    func splitAt(isSplit: (Iterator.Element) throws -> Bool) rethrows -> [SubSequence] {
        var p = self.startIndex
        var result:[SubSequence] = try self.indices.flatMap {
            i in
            guard try isSplit(self[i]) else {
                return nil
            }
            defer {
                p = self.index(after: i)
            }
            return self[p...i]
        }
        if p != self.endIndex {
            result.append(suffix(from: p))
        }
        return result
    }
}

感谢Oisdk让我思考。

答案 1 :(得分:2)

此方法适用于workbooks(wb).anything ,而不是CollectionTypes,但它应该很容易适应:

String

您可以像这样使用它:

extension CollectionType {
  func splitAt(@noescape isSplit: Generator.Element throws -> Bool) rethrows ->  [SubSequence] {
    var p = startIndex
    return try indices
      .filter { i in try isSplit(self[i]) }
      .map { i in
        defer { p = i }
        return self[p..<i]
      } + [suffixFrom(p)]
  }
}

extension CollectionType where Generator.Element : Equatable {
  func splitAt(splitter: Generator.Element) -> [SubSequence] {
    return splitAt { el in el == splitter }
  }
}

或者,此版本使用for循环,并在分隔符后拆分

let sentence = "Hello, my name is oisdk. This should split: but only at punctuation!"

let puncSet = Set("!.,:".characters)

sentence
  .characters
  .splitAt(puncSet.contains)
  .map(String.init)

// ["Hello", ", my name is oisdk", ". This should split", ": but only at punctuation", "!"]

或者,如果您希望将最多Swift功能集成到一个函数(extension CollectionType { func splitAt(@noescape isSplit: Generator.Element throws -> Bool) rethrows -> [SubSequence] { var p = startIndex var result: [SubSequence] = [] for i in indices where try isSplit(self[i]) { result.append(self[p...i]) p = i.successor() } if p != endIndex { result.append(suffixFrom(p)) } return result } } extension CollectionType where Generator.Element : Equatable { func splitAt(splitter: Generator.Element) -> [SubSequence] { return splitAt { el in el == splitter } } } let sentence = "Hello, my name is oisdk. This should split: but only at punctuation!" let puncSet = Set("!.,:".characters) sentence .characters .splitAt(puncSet.contains) .map(String.init) // ["Hello,", " my name is oisdk.", " This should split:", " but only at punctuation!"] defer,协议扩展,邪恶throwsflatMap和Optionals中):

guard

答案 2 :(得分:0)

我来这里是为了寻找这个问题的答案。找不到我想要的东西,最终通过重复调用.split(...)来构建它,虽然它不是很优雅,但是您可以选择保留哪些定界符,哪些不保留。有人知道,有可能避免String <-> Substring转换的方法。

var input = """
    {All those moments will be (lost in time)},
    like tears [in rain](. ([(Time to)] die))
"""
var separator: Character = "!"
var output: [String] = []
repeat {
    let tokens = input.split(
        maxSplits: 1,
        omittingEmptySubsequences: false,
        whereSeparator: {
            switch $0 {
                case "{", "}", "(", ")", "[", "]": // preserve
                    separator = $0; return true
                case " ", "\n", ",", ".":          // omit
                    separator = " "; return true
                default:
                    return false
            }
        }
    )
    if tokens[0] != ""  { 
        output.append(String(tokens[0])) 
    }
    guard tokens.count == 2 else { break }
    if separator != " " { 
        output.append(String(separator)) 
    }
    input = String(tokens[1])
} while true

for token in output { print("\(token)") }

在上述情况下,选择器不在实际集中。我不需要,但如果需要,只需进行以下声明,

let preservedDelimiters: Set<Character> = [ "{", "}", "(", ")", "[", "]" ]
let omittedDelimiters: Set<Character> = [ " ", "\n", ",", "." ]

,并将whereSeparator函数替换为:

whereSeparator: {
    if preservedDelimiters.contains($0) {
        separator = $0
        return true
    } else if omittedDelimiters.contains($0) {
        separator = " "
        return true
    } else {
        return false
    }
}