Add string split by components extension

This commit is contained in:
Daniel Saidi 2021-09-08 10:36:08 +02:00
parent b69f53eb4e
commit a65f0bc37c
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,17 @@
//
// String+Split.swift
// SwiftKit
//
// Created by Daniel Saidi on 2021-08-23.
// Copyright © 2021 Daniel Saidi. All rights reserved.
//
import Foundation
public extension String {
func split(by separators: [String]) -> [String] {
let separators = CharacterSet(charactersIn: separators.joined())
return components(separatedBy: separators)
}
}

View File

@ -0,0 +1,27 @@
//
// String+SplitTests.swift
// SwiftKitTests
//
// Created by Daniel Saidi on 2021-09-08.
// Copyright © 2021 Daniel Saidi. All rights reserved.
//
import Quick
import Nimble
import SwiftKit
class String_SplitTests: QuickSpec {
override func spec() {
describe("split by separators") {
it("splits on all provided separators") {
let string = "I.Love,Swift!Very much"
let result = string.split(by: [".", ",", "!"])
let expected = ["I", "Love", "Swift", "Very much"]
expect(result).to(equal(expected))
}
}
}
}