Add string split by components extension
This commit is contained in:
parent
b69f53eb4e
commit
a65f0bc37c
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue