String

struct String

This extesion adds some useful functions to String.

  • Returns the lenght of the string.

    Declaration

    Swift

    @available(*, deprecated: 3.2, renamed: "count", message: "`length` is deprecated and will be removed in a future version of BFKit-Swift.")
    public var length: Int { get }
  • Get the character at a given index.

    Declaration

    Swift

    public func character(at index: Int) -> Character

    Parameters

    index

    The index.

    Return Value

    Returns the character at a given index, starts from 0.

  • Returns a new string containing the characters of the String from the one at a given index to the end.

    Declaration

    Swift

    public func substring(from index: Int) -> String

    Parameters

    index

    The index.

    Return Value

    Returns the substring from index.

  • Creates a substring from the given character.

    Declaration

    Swift

    public func substring(from character: Character) -> String

    Parameters

    character

    The character.

    Return Value

    Returns the substring from character.

  • Returns a new string containing the characters of the String up to, but not including, the one at a given index.

    Declaration

    Swift

    public func substring(to index: Int) -> String

    Parameters

    index

    The index.

    Return Value

    Returns the substring to index.

  • Creates a substring to the given character.

    Declaration

    Swift

    public func substring(to character: Character) -> String

    Parameters

    character

    The character.

    Return Value

    Returns the substring to character.

  • Creates a substring with a given range.

    Declaration

    Swift

    public func substring(with range: Range<Int>) -> String

    Parameters

    range

    The range.

    Return Value

    Returns the string between the range.

  • Creates a substring with a given range.

    Declaration

    Swift

    public func substring(with range: CountableClosedRange<Int>) -> String

    Parameters

    range

    The range.

    Return Value

    Returns the string between the range.

  • Returns the index of the given character.

    Declaration

    Swift

    public func index(of character: Character) -> Int

    Parameters

    character

    The character to search.

    Return Value

    Returns the index of the given character, -1 if not found.

  • Check if self has the given substring in case-sensitiv or case-insensitive.

    Declaration

    Swift

    public func range(of string: String, caseSensitive: Bool = true) -> Bool

    Parameters

    string

    The substring to be searched.

    caseSensitive

    If the search has to be case-sensitive or not.

    Return Value

    Returns true if founded, otherwise false.

  • Check if self has the given substring in case-sensitiv or case-insensitive.

    Declaration

    Swift

    public func has(_ string: String, caseSensitive: Bool = true) -> Bool

    Parameters

    string

    The substring to be searched.

    caseSensitive

    If the search has to be case-sensitive or not.

    Return Value

    Returns true if founded, otherwise false.

  • Returns the number of occurrences of a String into

    Declaration

    Swift

    public func occurrences(of string: String, caseSensitive: Bool = true) -> Int

    Parameters

    string

    String of occurrences.

    Return Value

    Returns the number of occurrences of a String into

  • Conver self to a capitalized string. Example: This is a Test will return This is a test and this is a test will return This is a test.

    Declaration

    Swift

    public func sentenceCapitalizedString() -> String

    Return Value

    Returns the capitalized sentence string.

  • Returns the query parameter string.

    Declaration

    Swift

    func queryStringParameter(parameter: String) -> String?

    Parameters

    parameter

    Parameter to be searched.

    Return Value

    Returns the query parameter string.

  • Converts the query to a dictionary of parameters.

    Declaration

    Swift

    func queryDictionary() -> [String : String]

    Return Value

    Returns the dictionary of parameters.

  • Check if the URL is a valid HTTP URL.

    Declaration

    Swift

    public func isURLValid() -> Bool

    Return Value

    Returns if the URL is a valid HTTP URL

  • Convert a String to a NSAttributedString. With that variable you can customize a String with a style.

    Example:

    string.attributedString.font(UIFont(fontName: .helveticaNeue, size: 20))
    

    You can even concatenate two or more styles:

    string.attributedString.font(UIFont(fontName: .helveticaNeue, size: 20)).backgroundColor(UIColor.red)
    

    Declaration

    Swift

    public var attributedString: NSAttributedString { get }
  • Returns the last path component.

    Declaration

    Swift

    public var lastPathComponent: String { get }
  • Returns the path extension.

    Declaration

    Swift

    public var pathExtension: String { get }
  • Delete the last path component.

    Declaration

    Swift

    public var deletingLastPathComponent: String { get }
  • Delete the path extension.

    Declaration

    Swift

    public var deletingPathExtension: String { get }
  • Returns an array of path components.

    Declaration

    Swift

    public var pathComponents: [String] { get }
  • Appends a path component to the string.

    Declaration

    Swift

    public func appendingPathComponent(_ path: String) -> String

    Parameters

    path

    Path component to append.

    Return Value

    Returns all the string.

  • Appends a path extension to the string.

    Declaration

    Swift

    public func appendingPathExtension(_ ext: String) -> String?

    Parameters

    ext

    Extension to append.

    Return Value

    Returns all the string.

  • Converts self to an UUID APNS valid (No <> or - or spaces).

    Declaration

    Swift

    public func readableUUID() -> String

    Return Value

    Converts self to an UUID APNS valid (No <> or - or spaces).

  • Returns string with the first character uppercased.

    Declaration

    Swift

    public func uppercasedFirst() -> String

    Return Value

    Returns string with the first character uppercased.

  • Returns string with the first character lowercased.

    Declaration

    Swift

    public func lowercasedFirst() -> String

    Return Value

    Returns string with the first character lowercased.

  • Returns the reversed String.

    Declaration

    Swift

    public func reversed(preserveFormat: Bool) -> String

    Parameters

    preserveFormat

    If set to true preserve the String format. The default value is false. Example: Let’s try this function? -> ?noitcnuf siht yrt S'tel

    Return Value

    Returns the reversed String.

  • Returns true if the String has at least one uppercase chatacter, otherwise false.

    Declaration

    Swift

    public func hasUppercasedCharacters() -> Bool

    Return Value

    Returns true if the String has at least one uppercase chatacter, otherwise false.

  • Returns true if the String has at least one lowercase chatacter, otherwise false.

    Declaration

    Swift

    public func hasLowercasedCharacters() -> Bool

    Return Value

    Returns true if the String has at least one lowercase chatacter, otherwise false.

  • Remove double or more duplicated spaces.

    Declaration

    Swift

    public func removeExtraSpaces() -> String

    Return Value

    Remove double or more duplicated spaces.

  • Returns a new string in which all occurrences of a target strings in a specified range of the String are replaced by another given string.

    Declaration

    Swift

    public func replacingOccurrences(of target: [String], with replacement: String) -> String

    Parameters

    target

    Target strings array.

    replacement

    Replacement string.

    Return Value

    Returns a new string in which all occurrences of a target strings in a specified range of the String are replaced by another given string.

  • Count the number of lowercase characters.

    Declaration

    Swift

    public func countLowercasedCharacters() -> Int

    Return Value

    Number of lowercase characters.

  • Count the number of uppercase characters.

    Declaration

    Swift

    public func countUppercasedCharacters() -> Int

    Return Value

    Number of uppercase characters.

  • Count the number of numbers.

    Declaration

    Swift

    public func countNumbers() -> Int

    Return Value

    Number of numbers.

  • Count the number of symbols.

    Declaration

    Swift

    public func countSymbols() -> Int

    Return Value

    Number of symbols.

  • Convert HEX string (separated by space) to usual characters string. Example: 68 65 6c 6c 6f -> hello.

    Declaration

    Swift

    public func stringFromHEX() -> String

    Return Value

    Readable string.

  • Return if self is anagram of another String.

    Declaration

    Swift

    public func isAnagram(of string: String) -> Bool

    Parameters

    string

    Other String.

    Return Value

    Return true if self is anagram of another String, otherwise false.

  • Returns if self is palindrome.

    Declaration

    Swift

    public func isPalindrome() -> Bool

    Return Value

    Returns true if self is palindrome, otherwise false.

  • Returns the character at the given index.

    Declaration

    Swift

    public subscript(index: Int) -> Character { get }

    Parameters

    index

    Returns the character at the given index.

  • Returns the index of the given character, -1 if not found.

    Declaration

    Swift

    public subscript(character: Character) -> Int { get }

    Parameters

    character

    Returns the index of the given character, -1 if not found.

  • Returns the character at the given index as String.

    Declaration

    Swift

    public subscript(index: Int) -> String { get }

    Parameters

    index

    Returns the character at the given index as String.

  • Returns the string from a given range. Example: print(BFKit[1…3]) the result is FKi.

    Declaration

    Swift

    public subscript(range: Range<Int>) -> String { get }

    Parameters

    range

    Returns the string from a given range.

  • Returns if self is a valid UUID or not.

    Declaration

    Swift

    public func isUUID() -> Bool

    Return Value

    Returns if self is a valid UUID or not.

  • Returns if self is a valid UUID for APNS (Apple Push Notification System) or not.

    Declaration

    Swift

    public func isUUIDForAPNS() -> Bool

    Return Value

    Returns if self is a valid UUID for APNS (Apple Push Notification System) or not.

  • Returns a new string containing matching regular expressions replaced with the template string.

    Throws

    Throws NSRegularExpression(pattern:, options:) errors.

    Declaration

    Swift

    public func replacingMatches(regex regexString: String, with replacement: String) throws -> String

    Parameters

    regexString

    The regex string.

    replacement

    The replacement string.

    Return Value

    Returns a new string containing matching regular expressions replaced with the template string.

  • Localize current String using self as key.

    Declaration

    Swift

    public func localize() -> String

    Return Value

    Returns localized String using self as key.

  • Check if self is an email.

    Declaration

    Swift

    public func isEmail() -> Bool

    Return Value

    Returns true if it is an email, otherwise false.

  • Returns an array of String with all the links in

    Throws

    Throws NSDataDetector errors.

    Declaration

    Swift

    public func links() throws -> [String]

    Return Value

    Returns an array of String with all the links in

  • Returns an array of Date with all the dates in

    Throws

    Throws NSDataDetector errors.

    Declaration

    Swift

    public func dates() throws -> [Date]

    Return Value

    Returns an array of Date with all the date in

  • Returns an array of String with all the hashtags in

    Throws

    Throws NSRegularExpression errors.

    Declaration

    Swift

    public func hashtags() throws -> [String]

    Return Value

    Returns an array of String with all the hashtags in

  • Returns an array of String with all the mentions in

    Throws

    Throws NSRegularExpression errors.

    Declaration

    Swift

    public func mentions() throws -> [String]

    Return Value

    Returns an array of String with all the mentions in