String

struct String
  • Mechanica

    Returns the length of the String.

    Declaration

    Swift

    public var length: Int
  • Mechanica

    Reverse self.

    Declaration

    Swift

    public mutating func reverse()
  • Mechanica

    Returns true if self starts with a given prefix.

    Declaration

    Swift

    public func starts(with prefix: String, caseSensitive: Bool = true) -> Bool
  • Mechanica

    Returns true if self ends with a given suffix.

    Declaration

    Swift

    public func ends(with suffix: String, caseSensitive: Bool = true) -> Bool
  • Mechanica

    Checks if self contains a String.

    Declaration

    Swift

    public func contains(_ string: String, caseSensitive: Bool) -> Bool

    Return Value

    true if contains match, otherwise false.

  • Mechanica

    Checks if if all the characters in the string belong to a specific CharacterSet.

    Declaration

    Swift

    public func containsCharacters(in characterSet: CharacterSet) -> Bool

    Return Value

    true if all the characters in the string belong to the CharacterSet, otherwise false.

  • Mechanica

    Returns a new string in which all occurrences of a target are replaced by another given string.

    Declaration

    Swift

    public func replace(_ target: String, with replacement: String, caseSensitive: Bool = true) -> String
  • Mechanica

    Returns a percent-escaped string following RFC 3986 for a query string key or value.

    Declaration

    Swift

    public var urlEscaped: String?
  • Mechanica

    If self is a semantic version, returns a tuple with major, minor and patch components.

    Declaration

    Swift

    public var semanticVersion: (Int, Int, Int)
  • Mechanica

    Generates a new random alphanumeric string of a given length (default 8).

    Declaration

    Swift

    public static func random(length: UInt32 = 8) -> String
  • Mechanica

    Removes spaces and new lines from both ends of `self.

    Declaration

    Swift

    public mutating func trim()
  • Mechanica*

    Returns a new String made by removing spaces and new lines from both ends.

    Declaration

    Swift

    public func trimmed() -> String
  • Mechanica*

    Strips the specified characters from the beginning of self.

    Declaration

    Swift

    public func trimmedStart(characterSet set: CharacterSet = .whitespacesAndNewlines) -> String

    Return Value

    stripped string

  • Mechanica*

    Strips the specified characters from the end of self.

    Declaration

    Swift

    public func trimmedEnd(characterSet set: CharacterSet = .whitespacesAndNewlines) -> String

    Return Value

    stripped string

  • Mechanica

    Produces a new string with the first character of the first word changed to the corresponding uppercase value.

    Declaration

    Swift

    public func capitalizedFirstCharacter() -> String
  • Mechanica

    Produces a new string with the first character of the first word changed to the corresponding uppercase value.

    Declaration

    Swift

    public func decapitalizedFirstCharacter() -> String
  • Mechanica

    Returns a new string containing the first character of the String.

    Declaration

    Swift

    public var first: String
  • Mechanica

    Returns a new string containing the last character of the String.

    Declaration

    Swift

    public var last: String
  • Mechanica*

    Returns a substring, up to maxLength in length, containing the initial elements of the String.

    Warning

    If maxLength exceeds self.count, the result contains all the elements of self.

    Declaration

    Swift

    public func prefix(maxLength: Int) -> String
  • Mechanica*

    Returns a slice, up to maxLength in length, containing the final elements of String.

    Warning

    If maxLength exceeds String character count, the result contains all the elements of String.

    Declaration

    Swift

    public func suffix(maxLength: Int) -> String
  • Mechanica

    Returns a new String containing the characters of the String from the one at a given position to the end.

    Example:

    "hello".removingPrefix(upToPosition: 1) -> "ello"
    

    Declaration

    Swift

    public func removingPrefix(upToPosition: Int = 1) -> String
  • Mechanica

    Returns a new String removing the spcified prefix (if the string has it).

    Example:

    "hello".removingPrefix("hel") -> "lo"
    

    Declaration

    Swift

    public func removingPrefix(_ prefix: String) -> String
  • Mechanica

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

    Example:

    "hello".removingSuffix(fromPosition: 1) -> "hell"
    

    Declaration

    Swift

    public func removingSuffix(fromPosition: Int = 1) -> String
  • Mechanica

    Returns a new String removing the spcified suffix (if the string has it).

    Example:

    "hello".removingSuffix("0") -> "hell"
    

    Declaration

    Swift

    public func removingSuffix(_ suffix: String) -> String
  • Mechanica*

    Remove the characters in the given set.

    Declaration

    Swift

    public mutating func removeCharacters(in set: CharacterSet)
  • Mechanica*

    Returns a new String removing the characters in the given set.

    Declaration

    Swift

    public func removingCharacters(in set: CharacterSet) -> String
  • Mechanica*

    Truncates the String to the given length (number of characters) and appends optional trailing string if longer. The default trailing is the ellipsis (…).

    Declaration

    Swift

    public func truncate(at length: Int, withTrailing trailing: String? = "…") -> String
  • Mechanica

    Condenses all white spaces repetitions in a single white space. White space at the beginning or ending of the String are trimmed out.

    Example:

    let aString = "test    too many    spaces"
    aString.removeExcessiveSpaces  -> test too many spaces
    

    Declaration

    Swift

    public func condensingExcessiveSpaces() -> String

    Return Value

    A new string where all white spaces repetitions are replaced with a single white space.

  • Mechanica*

    Condenses all white spaces and new lines repetitions in a single white space. White space and new lines at the beginning or ending of the String are trimmed out.

    Declaration

    Swift

    public func condensingExcessiveSpacesAndNewlines() -> String

    Return Value

    A new string where all white spaces and new lines repetitions are replaced with a single white space.

  • Mechanica

    Returns a new string in which the characters in a specified CountableClosedRange range of the String are replaced by a given string.

    Declaration

    Swift

    public func replacingCharacters(in range: CountableClosedRange<Int>, with replacement: String) -> String
  • Mechanica

    Returns a new string in which the characters in a specified CountableRange range of the String are replaced by a given string.

    Declaration

    Swift

    public func replacingCharacters(in range: CountableRange<Int>, with replacement: String) -> String
  • Mechanica*

    Gets the character at the specified index as String.

    Declaration

    Swift

    public subscript (index: Int) -> String?

    Return Value

    Character as String or nil if the index is out of bounds

  • Mechanica*

    Returns the substring in the given range.

    Declaration

    Swift

    public subscript (range: Range<Int>) -> String?

    Return Value

    Substring in Range or nil.

  • Mechanica*

    Returns the substring in the given NSRange

    Declaration

    Swift

    public subscript (nsRange: NSRange) -> String?

    Return Value

    Substring in NSRange or nil.

  • Mechanica*

    Returns the range of the first occurrence of a given string in the String.

    Declaration

    Swift

    public subscript (substring: String) -> Range<String.Index>?

    Return Value

    range of the first occurrence or nil.

  • Mechanica

    Produces a camel cased version of the String.

    Example:

    let string = "HelloWorld"
    print(string.camelCased()) -> "helloWorld"
    

    Declaration

    Swift

    public func camelCased() -> String

    Return Value

    A camel cased copy of the String.

  • Mechanica

    Produces the kebab cased version of the String.

    Example:

    let string = "Hello World"
    print(string.kebabCased()) -> "-Hello-World-"
    

    Declaration

    Swift

    public func kebabCased() -> String

    Return Value

    The kebab cased copy of the String.

  • Mechanica

    Produces a pascal cased version of the String.

    Example:

    let string = "HELLO WORLD"
    print(string.pascalCased()) -> "HelloWorld"
    

    Declaration

    Swift

    public func pascalCased() -> String

    Return Value

    A pascal cased copy of the String.

  • Mechanica

    Produces the slug version of the String.

    Example:

    let string = "Hello World"
    print(string.slugCased()) -> "Hello-World"
    

    Declaration

    Swift

    public func slugCased() -> String

    Return Value

    The slug copy of the String.

  • Mechanica

    Produces the snake cased version of the String.

    Example:

    let string = "hello world"
    print(string.snakeCased())
    -> Prints "hello_world"
    

    Declaration

    Swift

    public func snakeCased() -> String

    Return Value

    The slug copy of the String.

  • Mechanica

    Produces the swap cased version of the String.

    Example:

    let string = "Hello World"
    print(string.swapCased()) -> "hELLO wORLD"
    

    Declaration

    Swift

    public func swapCased() -> String

    Return Value

    The swap cased copy of the String.

  • Mechanica

    Returns the NSRange of self.

    Declaration

    Swift

    public var nsRange: NSRange
  • Mechanica

    Returns a new String with the center-padded version of self if it’s shorter than length using a token. Padding characters are truncated if they can’t be evenly divided by length.

    Example 1:

    let string = "Hello World"
    string.padding(length: 15) -> "  Hello World  "
    

    Example 2:

    let string = "Hello World"
    string.padding(length: 15, withToken: "*") -> "**Hello World**"
    

    Note

    If the the sum-total of characters added is odd, the left side of the string will have one less instance of the token.

    Declaration

    Swift

    public func padding(length: Int, with token: String = " ") -> String

    Return Value

    The padded copy of the string.

  • Mechanica

    Pads self on the left and right sides if it's shorter thanlengthusing atoken`. Padding characters are truncated if they can’t be evenly divided by length.

    Example 1:

    let string = "Hello World"
    string.pad(length: 15) -> "  Hello World  "
    

    Example 2:

    let string = "Hello World"
    string.pad(length: 15, withToken: "*") -> "**Hello World**"
    

    Note

    If the the sum-total of characters added is odd, the left side of the string will have one less instance of the token.

    Declaration

    Swift

    public mutating func pad(length: Int, with token: String = " ")
  • Mechanica

    Returns a new String with the left-padded version of self if it’s shorter than length using a token. Padding characters are truncated if they can’t be evenly divided by length.

    Example 1:

    let string = "Hello World"
    string.paddingStart(length: 15) -> "    Hello World"
    

    Example 2:

    let string = "Hello World"
    string.paddingStart(length: 15, withToken: "*") -> "****Hello World"
    

    Declaration

    Swift

    public func paddingStart(length: Int, with token: String = " ") -> String

    Return Value

    The left-padded copy of the string.

  • Mechanica

    Pads self on the left side if it’s shorter than length using a token. Padding characters are truncated if they exceed length.

    Example 1:

    let string = "Hello World"
    string.padStart(length: 15) -> "    Hello World"
    

    Example 2:

    let string = "Hello World"
    string.padStart(length: 15, withToken: "*") -> "****Hello World"
    

    Declaration

    Swift

    public mutating func padStart(length: Int, with token: String = " ")
  • Mechanica

    Returns a new String with the right-padded version of self if it’s shorter than length using a token. Padding characters are truncated if they can’t be evenly divided by length.

    Example 1:

    let string = "Hello World"
    string.paddingEnd(length: 15) -> "Hello World    "
    

    Example 2:

    let string = "Hello World"
    string.paddingEnd(length: 15, withToken: "*", ) -> "Hello World****"
    

    Declaration

    Swift

    public func paddingEnd(length: Int, with token: String = " ") -> String

    Return Value

    The right-padded copy of the string.

  • Mechanica

    Pads self on the right side if it’s shorter than length using a token. Padding characters are truncated if they exceed length.

    Example 1:

    let string = "Hello World"
    string.padEnd(length: 15) -> "Hello World    "
    

    Example 2:

    let string = "Hello World"
    string.padEnd(length: 15, withToken: "*", ) -> "Hello World****"
    

    Declaration

    Swift

    public mutating func padEnd(length: Int, with token: String = " ")
  • Mechanica

    Declaration

    Swift

    public func ranges(matching pattern: String, options: NSRegularExpression.Options = []) -> [Range<String.Index>]

    Return Value

    returns a list of matched ranges for self.

  • Mechanica

    Declaration

    Swift

    public func firstRange(matching pattern: String, options: NSRegularExpression.Options = []) -> Range<String.Index>?

    Return Value

    returns a the first matched range for self or nil.

  • Mechanica

    Creates a new string representing the given string repeated the specified number of times.

    Declaration

    Swift

    public static func * (lhs: String, rhs: Int) -> String
  • Mechanica

    Creates a new string representing the given string repeated the specified number of times.

    Declaration

    Swift

    public static func * (lhs: Int, rhs: String) -> String
  • Mechanica

    Return a Bool value by parsing self.

    Declaration

    Swift

    public var bool: Bool?
  • Mechanica

    Returns a new string decoded from base64.

    Declaration

    Swift

    public var base64Decoded: String?
  • Mechanica

    Returns a new string encoded in base64.

    Declaration

    Swift

    public var base64Encoded: String?
  • Mechanica

    Returns true if the String contains one or more letters.

    Declaration

    Swift

    public var hasLetters: Bool
  • Mechanica

    Returns true if the String contains one or more numbers.

    Declaration

    Swift

    public var hasNumbers: Bool
  • Mechanica

    Returns true if the String contains only letters.

    Declaration

    Swift

    public var isAlphabetic: Bool
  • Mechanica

    Checks if the String contains only numbers.

    Declaration

    Swift

    public var isNumeric: Bool
  • Mechanica

    Returns true if the String contains at least one letter and one number.

    Declaration

    Swift

    public var isAlphaNumeric: Bool
  • Mechanica

    Returns true if all the characters are lowercased.

    Declaration

    Swift

    public var isLowercased: Bool
  • Mechanica

    Returns true, if all characters are uppercased. Otherwise, false.

    Declaration

    Swift

    public var isUppercased: Bool
  • Mechanica

    Checks if the String is blank (a string that is either empty or contains only space/newline characters).

    Declaration

    Swift

    public var isBlank: Bool
  • Mechanica

    Checks if all of the characters in a string are all the same.

    Declaration

    Swift

    public var isHomogeneous: Bool
  • Mechanica

    Checks if string is a valid URL.

    Example:

    "https://tinrobots.org".isValidUrl -> true
    

    Declaration

    Swift

    public var isValidUrl: Bool
  • Mechanica

    Check if the String is a valid schemed URL.

    Example:

    "https://tinrobots.org".isValidSchemedUrl -> true
    "tinrobots.org".isValidSchemedUrl -> false
    

    Declaration

    Swift

    public var isValidSchemedUrl: Bool
  • Mechanica

    Checks if string is a valid https URL.

    Example:

    "https://tinrobots.org".isValidHttpsUrl -> true
    

    Declaration

    Swift

    public var isValidHttpsUrl: Bool
  • Mechanica

    Checks if string is a valid http URL.

    Example:

    "http://tinrobots.org".isValidHttpUrl -> true
    

    Declaration

    Swift

    public var isValidHttpUrl: Bool
  • Mechanica

    Checks if string is a valid file URL.

    Example:

    "file://Documents/file.txt".isValidFileUrl -> true
    

    Declaration

    Swift

    public var isValidFileUrl: Bool
  • Mechanica

    Returns true if the String is a valid email.

    Seealso

    link 1

    Seealso

    link 2

    Declaration

    Swift

    public var isValidEmail: Bool
  • Mechanica

    Returns true if self is a country emoji flag.

    Note

    to check if a string contains a flag use: self.contains { $0.isFlag }

    Note

    to extrapolate the flags in a string use: self.filter { $0.isFlag }

    Declaration

    Swift

    public var isEmojiCountryFlag: Bool
  • Mechanica

    Checks if self is a semantic version with a value equal to a given version.

    Declaration

    Swift

    public func isSemanticVersionEqual(to version: String) -> Bool
  • Mechanica

    Checks if self is a semantic version with a value greater than given version.

    Declaration

    Swift

    public func isSemanticVersionGreater(than version: String) -> Bool
  • Mechanica

    Checks if self is a semantic version with a value greater or equal to a given version.

    Declaration

    Swift

    public func isSemanticVersionGreaterOrEqual(to version: String) -> Bool
  • Mechanica

    Checks if self is a semantic version with a value lesser than a given version.

    Declaration

    Swift

    public func isSemanticVersionLesser(than version: String) -> Bool
  • Mechanica

    Checks if self is a semantic version with a value lesser or equal to a given version.

    Declaration

    Swift

    public func isSemanticVersionLesserOrEqual(to version: String) -> Bool