Skip to content

Instantly share code, notes, and snippets.

@dmonagle
Last active July 19, 2022 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmonagle/2d23e23a4b67cb82db32fddf626987d5 to your computer and use it in GitHub Desktop.
Save dmonagle/2d23e23a4b67cb82db32fddf626987d5 to your computer and use it in GitHub Desktop.
extension String {
public func splitLastWhitespace(after offset: Int) -> [Substring] {
guard let lastWS = self.prefix(offset).lastIndex(where: \.isWhitespace) else {
return [self[self.startIndex...]]
}
let first = self[self.startIndex..<lastWS]
let nextIndex = self.index(after: lastWS)
guard nextIndex != self.endIndex else { return [first] }
return [first, self[nextIndex...]]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment