小编典典

在Swift中拆分字符串而不删除定界符

swift

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

componentsSeparatedByCharactersInSet删除定界符。如果仅用一个可能的字符分开,则很容易将其重新添加。但是当你有一套的时候呢?

还有其他分割方法吗?


阅读 286

收藏
2020-07-07

共1个答案

小编典典

此方法适用于CollectionTypes,而不是Strings,但是它应该很容易适应:

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 }
  }
}

您可以这样使用它:

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

或者,此版本使用for循环,并 定界符 拆分:

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

或者,如果你想获得最斯威夫特功能为一体的功能(deferthrows,一个协议扩展,一个邪恶的flatMapguard以及选配):

extension CollectionType {
  func splitAt(@noescape isSplit: Generator.Element throws -> Bool) rethrows -> [SubSequence] {
    var p = startIndex
    var result: [SubSequence] = try indices.flatMap { i in
      guard try isSplit(self[i]) else { return nil }
      defer { p = i.successor() }
      return self[p...i]
    }
    if p != endIndex { result.append(suffixFrom(p)) }
    return result
  }
}
2020-07-07