String

public extension String
  • Find all matches found in the given string.

    Declaration

    Swift

    func allMatches(with capturePattern: String) -> [String]

    Parameters

    capturePattern

    String pattern containing optional capturing groups

    Return Value

    [String] where the first element is the matching string and the following elements are the substrings that matched the capturing groups (if any)

  • Returns an array of substrings in self that matched the regex pattern

    Declaration

    Swift

    @available(*, deprecated, message: "use allMatches(with:﹚ instead")
    func matches(for regex: String) -> [String]

    Parameters

    regex

    String pattern

    Return Value

    [String] Note: deprecated because it does not handle capture groups

  • Test whether self matches the regex pattern

    Declaration

    Swift

    func doesMatch(regex: String) -> Bool

    Parameters

    regex

    String pattern

    Return Value

    Bool, true if self matches the regex pattern

  • Replace parts matched by pattern by replacement in self

    • Example “Hello World!”.replace(pattern: “Hello (.+)”, replacement: “Goodbye $1”) -> “Goodbye World!”

    Declaration

    Swift

    func replace(pattern: String, replacement: String) -> String

    Parameters

    pattern

    String, pattrrn to match (may contain capture groups)

    replacement

    String (may contain backreferences $1,… to captured matches)

    Return Value

    String, a modified copy of self

extension String: specific case methods that use regex

  • Check if self is blank (is empty or consists of whitespace characters only)

    Declaration

    Swift

    func isBlank() -> Bool

    Return Value

    true if self is blank

  • Returns UUID (if any) found in self

    Declaration

    Swift

    func extractUUID() -> String?

    Return Value

    String that matched the UUID pattern or nil

  • Returns the first number found in self, or nil Expected self to contain at least one substring representing an integer or a fixed point double, possibly surrounded by non-numeric characters. Example: “{\"USD\”:57938.29}“

    Declaration

    Swift

    func extractDouble() -> Double?
  • Returns a tuple of currency code and amount. Expects self to be a price string consisting of a 3-letter currency code and an integer or decimal number, in either order, separated by whitespace characters, for example “USD 349.95” or “234.50 EUR”. See https://en.wikipedia.org/wiki/ISO_4217 Moreover, tolerates and removes a thousands separator “,” if present in self. for exmple “CHF 1,233.00”

    Declaration

    Swift

    func extractCurrencyAndAmount() -> (String, Double)?
  • Returns a copy of self with 1st letter capitalized

    Declaration

    Swift

    func capitalizingFirstLetter() -> String
  • Capitalizes 1st letter

    Declaration

    Swift

    mutating func capitalizeFirstLetter()
  • Capitalizes 1st letter and inserts a space before any other capital letter

    Declaration

    Swift

    var camelCaseSplit: String { get }
  • Localizes a text string

    Declaration

    Swift

    func localized(bundle _: Bundle = .main, tableName: String = "Localizable") -> String

    Parameters

    _

    bundle

    tableName

    table

    Return Value

    localized version of self (if self found in localization tables as a key)