Skip to content

Instantly share code, notes, and snippets.

@acrookston
Last active April 5, 2021 06:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrookston/2345f4ddb474e2c2c99ae5852fbc1afe to your computer and use it in GitHub Desktop.
Save acrookston/2345f4ddb474e2c2c99ae5852fbc1afe to your computer and use it in GitHub Desktop.
trim, strip and split, regex for Swift String
extension NSRegularExpression {
convenience init(substrings: [String], options: NSRegularExpression.Options) throws {
let escapedSubstrings: [String] = substrings.map(NSRegularExpression.escapedTemplate)
let pattern: String = escapedSubstrings.joined(separator: "|")
try self.init(pattern: pattern, options: options)
}
convenience init?(with pattern: String, options: NSRegularExpression.Options = []) {
do {
try self.init(pattern: pattern, options: options)
} catch {
return nil
}
}
func match(in input: String) -> Bool {
return numberOfMatches(in: input, options: [], range: input.range) > 0
}
}
extension String {
/// Trims whitespace from start and end of string
///
/// - Returns: self with whitespace removed from start and end
///
func trim() -> String {
return trimmingCharacters(in: .whitespaces)
}
/// Strips (or removes) given character from string
///
/// - Parameters:
/// - character: String of one character to be removed
///
/// - Returns: self with character removed from string
///
func strip(_ character: String) -> String {
return replacingOccurrences(of: character, with: "")
}
/// Strips (or removes) several characters from string
///
/// - Parameters:
/// - characters: Characters you want to be removed (one character per string!)
///
/// - Returns: self with characters removed from string
///
func strip(_ characters: [String]) -> String {
var output = self
for character in characters {
output = replacingOccurrences(of: character, with: "")
}
return output
}
/// Splits a string into an array of strings with specifed components
/// Starts at the beginning of the string and keeps splitting until end of string OR end of components.
///
/// - Parameters:
/// - components: length of each desired substring
///
/// - Returns: array of strings split into each given component lenght
///
/// ```
/// "12345".split([1, 2, 3]) == ["1", "23", "45"]
/// ```
///
func split(_ components: [Int]) -> [String?] {
let maxIndex = index(startIndex, offsetBy: count)
return components.enumerated().map { idx, length in
let start = components[0..<idx].reduce(0, +)
guard let startIndex = index(startIndex, offsetBy: start, limitedBy: maxIndex) else {
return nil
}
if startIndex == maxIndex {
return nil
}
let endIndex = index(startIndex, offsetBy: length, limitedBy: maxIndex) ?? maxIndex
return String(self[startIndex..<endIndex])
}
}
var range: NSRange { return NSRange(location: 0, length: count) }
func matches(pattern: String, options: NSRegularExpression.Options = []) -> Bool {
return NSRegularExpression(with: pattern, options: options)?.match(in: self) ?? false
}
func matches(regex: NSRegularExpression) -> Bool {
return regex.match(in: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment