String

struct String
  • SwifterSwift: Number of characters in string.

    "Hello world!".length -> 12
    

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, message: "Use .count instead.")
    public var length: Int { get }
  • SwifterSwift: Sliced string from a start index.

    "Hello World".slicing(at: 6) -> "World"
    

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, message: "Use string[safe: i] instead.")
    public func slicing(at i: Int) -> String?

    Parameters

    i

    string index the slicing should start from.

    Return Value

    sliced substring starting from start index (if applicable) (example: Hello world.slicing(at: 6) -> world)

  • SwifterSwift: Sliced string from a start index to an end index.

    "Hello World".slicing(from: 6, to: 11) -> "World"
    

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, message: "Use string[safe: start..<end] instead.")
    public func slicing(from start: Int, to end: Int) -> String?

    Parameters

    start

    string index the slicing should start from.

    end

    string index the slicing should end at.

    Return Value

    sliced substring starting from start index, and ends at end index (if applicable) (example: Hello World.slicing(from: 6, to: 11) -> World)

  • SwifterSwift: First index of substring in string.

    "Hello World!".firstIndex(of: "l") -> 2
    "Hello World!".firstIndex(of: "s") -> nil
    

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, message: "Use string.index(of: Character﹚ or string.range(of: StringProtocol﹚ instead.")
    public func firstIndex(of string: String) -> Int?

    Parameters

    string

    substring to search for.

    Return Value

    first index of substring in string (if applicable).

  • SwifterSwift: Array of strings separated by given string.

    "hello World".splited(by: " ") -> ["hello", "World"]
    

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, message: "Use string.split(separator: Character﹚ instead.")
    public func splitted(by separator: Character) -> [String]

    Parameters

    separator

    separator to split string by.

    Return Value

    array of strings separated by given string.

  • SwifterSwift: String decoded from base64 (if applicable).

    "SGVsbG8gV29ybGQh".base64Decoded = Optional("Hello World!")
    

    Declaration

    Swift

    public var base64Decoded: String? { get }
  • SwifterSwift: String encoded in base64 (if applicable).

    "Hello World!".base64Encoded -> Optional("SGVsbG8gV29ybGQh")
    

    Declaration

    Swift

    public var base64Encoded: String? { get }
  • SwifterSwift: Array of characters of a string.

    Declaration

    Swift

    public var charactersArray: [Character] { get }
  • SwifterSwift: CamelCase of string.

    "sOme vAriable naMe".camelCased -> "someVariableName"
    

    Declaration

    Swift

    public var camelCased: String { get }
  • SwifterSwift: Check if string contains one or more emojis.

    "Hello 😀".containEmoji -> true
    

    Declaration

    Swift

    public var containEmoji: Bool { get }
  • SwifterSwift: First character of string (if applicable).

    "Hello".firstCharacterAsString -> Optional("H")
    "".firstCharacterAsString -> nil
    

    Declaration

    Swift

    public var firstCharacterAsString: String? { get }
  • SwifterSwift: Check if string contains one or more letters.

    "123abc".hasLetters -> true
    "123".hasLetters -> false
    

    Declaration

    Swift

    public var hasLetters: Bool { get }
  • SwifterSwift: Check if string contains one or more numbers.

    "abcd".hasNumbers -> false
    "123abc".hasNumbers -> true
    

    Declaration

    Swift

    public var hasNumbers: Bool { get }
  • SwifterSwift: Check if string contains only letters.

    "abc".isAlphabetic -> true
    "123abc".isAlphabetic -> false
    

    Declaration

    Swift

    public var isAlphabetic: Bool { get }
  • SwifterSwift: Check if string contains at least one letter and one number.

    // useful for passwords
    "123abc".isAlphaNumeric -> true
    "abc".isAlphaNumeric -> false
    

    Declaration

    Swift

    public var isAlphaNumeric: Bool { get }
  • SwifterSwift: Check if string is valid email format.

    "[email protected]".isEmail -> true
    

    Declaration

    Swift

    public var isEmail: Bool { get }
  • SwifterSwift: Check if string is a valid URL.

    "https://google.com".isValidUrl -> true
    

    Declaration

    Swift

    public var isValidUrl: Bool { get }
  • SwifterSwift: Check if string is a valid schemed URL.

    "https://google.com".isValidSchemedUrl -> true
    "google.com".isValidSchemedUrl -> false
    

    Declaration

    Swift

    public var isValidSchemedUrl: Bool { get }
  • SwifterSwift: Check if string is a valid https URL.

    "https://google.com".isValidHttpsUrl -> true
    

    Declaration

    Swift

    public var isValidHttpsUrl: Bool { get }
  • SwifterSwift: Check if string is a valid http URL.

    "http://google.com".isValidHttpUrl -> true
    

    Declaration

    Swift

    public var isValidHttpUrl: Bool { get }
  • SwifterSwift: Check if string is a valid file URL.

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

    Declaration

    Swift

    public var isValidFileUrl: Bool { get }
  • SwifterSwift: Check if string is a valid Swift number.

    Note: In North America, . is the decimal separator, while in many parts of Europe , is used,

    "123".isNumeric -> true
    "1.3".isNumeric -> true (en_US)
    "1,3".isNumeric -> true (fr_FR)
    "abc".isNumeric -> false
    

    Declaration

    Swift

    public var isNumeric: Bool { get }
  • SwifterSwift: Check if string only contains digits.

    "123".isDigits -> true
    "1.3".isDigits -> false
    "abc".isDigits -> false
    

    Declaration

    Swift

    public var isDigits: Bool { get }
  • SwifterSwift: Last character of string (if applicable).

    "Hello".lastCharacterAsString -> Optional("o")
    "".lastCharacterAsString -> nil
    

    Declaration

    Swift

    public var lastCharacterAsString: String? { get }
  • SwifterSwift: Latinized string.

    "Hèllö Wórld!".latinized -> "Hello World!"
    

    Declaration

    Swift

    public var latinized: String { get }
  • SwifterSwift: Bool value from string (if applicable).

    "1".bool -> true
    "False".bool -> false
    "Hello".bool = nil
    

    Declaration

    Swift

    public var bool: Bool? { get }
  • SwifterSwift: Date object from yyyy-MM-dd formatted string.

    "2007-06-29".date -> Optional(Date)
    

    Declaration

    Swift

    public var date: Date? { get }
  • SwifterSwift: Date object from yyyy-MM-dd HH:mm:ss formatted string.

    "2007-06-29 14:23:09".dateTime -> Optional(Date)
    

    Declaration

    Swift

    public var dateTime: Date? { get }
  • int

    SwifterSwift: Integer value from string (if applicable).

    "101".int -> 101
    

    Declaration

    Swift

    public var int: Int? { get }
  • SwifterSwift: Lorem ipsum string of given length.

    Declaration

    Swift

    public static func loremIpsum(ofLength length: Int = 445) -> String

    Parameters

    length

    number of characters to limit lorem ipsum to (default is 445 - full lorem ipsum).

    Return Value

    Lorem ipsum dolor sit amet… string.

  • url

    SwifterSwift: URL from string (if applicable).

    "https://google.com".url -> URL(string: "https://google.com")
    "not url".url -> nil
    

    Declaration

    Swift

    public var url: URL? { get }
  • SwifterSwift: String with no spaces or new lines in beginning and end.

    "   hello  \n".trimmed -> "hello"
    

    Declaration

    Swift

    public var trimmed: String { get }
  • SwifterSwift: Readable string from a URL string.

    "it's%20easy%20to%20decode%20strings".urlDecoded -> "it's easy to decode strings"
    

    Declaration

    Swift

    public var urlDecoded: String { get }
  • SwifterSwift: URL escaped string.

    "it's easy to encode strings".urlEncoded -> "it's%20easy%20to%20encode%20strings"
    

    Declaration

    Swift

    public var urlEncoded: String { get }
  • SwifterSwift: String without spaces and new lines.

    "   \n Swifter   \n  Swift  ".withoutSpacesAndNewLines -> "SwifterSwift"
    

    Declaration

    Swift

    public var withoutSpacesAndNewLines: String { get }
  • SwifterSwift: Check if the given string contains only white spaces

    Declaration

    Swift

    public var isWhitespace: Bool { get }
  • SwifterSwift: Check if the given string spelled correctly

    Declaration

    Swift

    public var isSpelledCorrectly: Bool { get }
  • Float value from string (if applicable).

    Declaration

    Swift

    public func float(locale: Locale = .current) -> Float?

    Parameters

    locale

    Locale (default is Locale.current)

    Return Value

    Optional Float value from given string.

  • Double value from string (if applicable).

    Declaration

    Swift

    public func double(locale: Locale = .current) -> Double?

    Parameters

    locale

    Locale (default is Locale.current)

    Return Value

    Optional Double value from given string.

  • CGFloat value from string (if applicable).

    Declaration

    Swift

    public func cgFloat(locale: Locale = .current) -> CGFloat?

    Parameters

    locale

    Locale (default is Locale.current)

    Return Value

    Optional CGFloat value from given string.

  • SwifterSwift: Array of strings separated by new lines.

    "Hello\ntest".lines() -> ["Hello", "test"]
    

    Declaration

    Swift

    public func lines() -> [String]

    Return Value

    Strings separated by new lines.

  • SwifterSwift: Returns a localized string, with an optional comment for translators.

       "Hello world".localized -> Hallo Welt
    

    Declaration

    Swift

    public func localized(comment: String = "") -> String
  • SwifterSwift: The most common character in string.

    "This is a test, since e is appearing everywhere e should be the common character".mostCommonCharacter() -> "e"
    

    Declaration

    Swift

    public func mostCommonCharacter() -> Character?

    Return Value

    The most common character.

  • SwifterSwift: Array with unicodes for all characters in a string.

    "SwifterSwift".unicodeArray -> [83, 119, 105, 102, 116, 101, 114, 83, 119, 105, 102, 116]
    

    Declaration

    Swift

    public func unicodeArray() -> [Int]

    Return Value

    The unicodes for all characters in a string.

  • SwifterSwift: an array of all words in a string

    "Swift is amazing".words() -> ["Swift", "is", "amazing"]
    

    Declaration

    Swift

    public func words() -> [String]

    Return Value

    The words contained in a string.

  • SwifterSwift: Count of words in a string.

    "Swift is amazing".wordsCount() -> 3
    

    Declaration

    Swift

    public func wordCount() -> Int

    Return Value

    The count of words contained in a string.

  • SwifterSwift: Transforms the string into a slug string.

       "Swift is amazing".toSlug() -> "swift-is-amazing"
    

    Declaration

    Swift

    public func toSlug() -> String

    Return Value

    The string in slug format.

  • SwifterSwift: Safely subscript string with index.

    "Hello World!"[3] -> "l"
    "Hello World!"[20] -> nil
    

    Declaration

    Swift

    public subscript(safe i: Int) -> Character? { get }

    Parameters

    i

    index.

  • SwifterSwift: Safely subscript string within a half-open range.

    "Hello World!"[6..<11] -> "World"
    "Hello World!"[21..<110] -> nil
    

    Declaration

    Swift

    public subscript(safe range: CountableRange<Int>) -> String? { get }

    Parameters

    range

    Half-open range.

  • SwifterSwift: Safely subscript string within a closed range.

    "Hello World!"[6...11] -> "World!"
    "Hello World!"[21...110] -> nil
    

    Declaration

    Swift

    public subscript(safe range: ClosedRange<Int>) -> String? { get }

    Parameters

    range

    Closed range.

  • SwifterSwift: Copy string to global pasteboard.

    "SomeText".copyToPasteboard() // copies "SomeText" to pasteboard
    

    Declaration

    Swift

    public func copyToPasteboard()
  • SwifterSwift: Converts string format to CamelCase.

    var str = "sOme vaRiabLe Name"
    str.camelize()
    print(str) // prints "someVariableName"
    

    Declaration

    Swift

    public mutating func camelize()
  • SwifterSwift: Check if string contains only unique characters.

    Declaration

    Swift

    public func hasUniqueCharacters() -> Bool
  • SwifterSwift: Check if string contains one or more instance of substring.

    "Hello World!".contain("O") -> false
    "Hello World!".contain("o", caseSensitive: false) -> true
    

    Declaration

    Swift

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

    Parameters

    string

    substring to search for.

    caseSensitive

    set true for case sensitive search (default is true).

    Return Value

    true if string contains one or more instance of substring.

  • SwifterSwift: Count of substring in string.

    "Hello World!".count(of: "o") -> 2
    "Hello World!".count(of: "L", caseSensitive: false) -> 3
    

    Declaration

    Swift

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

    Parameters

    string

    substring to search for.

    caseSensitive

    set true for case sensitive search (default is true).

    Return Value

    count of appearance of substring in string.

  • SwifterSwift: Check if string ends with substring.

    "Hello World!".ends(with: "!") -> true
    "Hello World!".ends(with: "WoRld!", caseSensitive: false) -> true
    

    Declaration

    Swift

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

    Parameters

    suffix

    substring to search if string ends with.

    caseSensitive

    set true for case sensitive search (default is true).

    Return Value

    true if string ends with substring.

  • SwifterSwift: Latinize string.

    var str = "Hèllö Wórld!"
    str.latinize()
    print(str) // prints "Hello World!"
    

    Declaration

    Swift

    public mutating func latinize()
  • SwifterSwift: Random string of given length.

    String.random(ofLength: 18) -> "u7MMZYvGo9obcOcPj8"
    

    Declaration

    Swift

    public static func random(ofLength length: Int) -> String

    Parameters

    length

    number of characters in string.

    Return Value

    random string of given length.

  • SwifterSwift: Reverse string.

    Declaration

    Swift

    public mutating func reverse()
  • SwifterSwift: Sliced string from a start index with length.

       "Hello World".slicing(from: 6, length: 5) -> "World"
    

    Declaration

    Swift

    public func slicing(from i: Int, length: Int) -> String?

    Parameters

    i

    string index the slicing should start from.

    length

    amount of characters to be sliced after given index.

    Return Value

    sliced substring of length number of characters (if applicable) (example: Hello World.slicing(from: 6, length: 5) -> World)

  • SwifterSwift: Slice given string from a start index with length (if applicable).

    var str = "Hello World"
    str.slice(from: 6, length: 5)
    print(str) // prints "World"
    

    Declaration

    Swift

    public mutating func slice(from i: Int, length: Int)

    Parameters

    i

    string index the slicing should start from.

    length

    amount of characters to be sliced after given index.

  • SwifterSwift: Slice given string from a start index to an end index (if applicable).

    var str = "Hello World"
    str.slice(from: 6, to: 11)
    print(str) // prints "World"
    

    Declaration

    Swift

    public mutating func slice(from start: Int, to end: Int)

    Parameters

    start

    string index the slicing should start from.

    end

    string index the slicing should end at.

  • SwifterSwift: Slice given string from a start index (if applicable).

    var str = "Hello World"
    str.slice(at: 6)
    print(str) // prints "World"
    

    Declaration

    Swift

    public mutating func slice(at i: Int)

    Parameters

    i

    string index the slicing should start from.

  • SwifterSwift: Check if string starts with substring.

    "hello World".starts(with: "h") -> true
    "hello World".starts(with: "H", caseSensitive: false) -> true
    

    Declaration

    Swift

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

    Parameters

    suffix

    substring to search if string starts with.

    caseSensitive

    set true for case sensitive search (default is true).

    Return Value

    true if string starts with substring.

  • SwifterSwift: Date object from string of date format.

    "2017-01-15".date(withFormat: "yyyy-MM-dd") -> Date set to Jan 15, 2017
    "not date string".date(withFormat: "yyyy-MM-dd") -> nil
    

    Declaration

    Swift

    public func date(withFormat format: String) -> Date?

    Parameters

    format

    date format.

    Return Value

    Date object from string (if applicable).

  • SwifterSwift: Removes spaces and new lines in beginning and end of string.

    var str = "  \n Hello World \n\n\n"
    str.trim()
    print(str) // prints "Hello World"
    

    Declaration

    Swift

    public mutating func trim()
  • SwifterSwift: Truncate string (cut it to a given number of characters).

    var str = "This is a very long sentence"
    str.truncate(toLength: 14)
    print(str) // prints "This is a very..."
    

    Declaration

    Swift

    public mutating func truncate(toLength length: Int, trailing: String? = "...")

    Parameters

    toLength

    maximum number of characters before cutting.

    trailing

    string to add at the end of truncated string (default is ).

  • SwifterSwift: Truncated string (limited to a given number of characters).

    "This is a very long sentence".truncated(toLength: 14) -> "This is a very..."
    "Short sentence".truncated(toLength: 14) -> "Short sentence"
    

    Declaration

    Swift

    public func truncated(toLength length: Int, trailing: String? = "...") -> String

    Parameters

    toLength

    maximum number of characters before cutting.

    trailing

    string to add at the end of truncated string.

    Return Value

    truncated string (this is an extr…).

  • SwifterSwift: Convert URL string to readable string.

    var str = "it's%20easy%20to%20decode%20strings"
    str.urlDecode()
    print(str) // prints "it's easy to decode strings"
    

    Declaration

    Swift

    public mutating func urlDecode()
  • SwifterSwift: Escape string.

    var str = "it's easy to encode strings"
    str.urlEncode()
    print(str) // prints "it's%20easy%20to%20encode%20strings"
    

    Declaration

    Swift

    public mutating func urlEncode()
  • SwifterSwift: Verify if string matches the regex pattern.

    Declaration

    Swift

    public func matches(pattern: String) -> Bool

    Parameters

    pattern

    Pattern to verify.

    Return Value

    true if string matches the pattern.

  • SwifterSwift: Pad string to fit the length parameter size with another string in the start.

    hue.padStart(10) -> hue hue.padStart(10, with: br) -> brbrbrbhue

    Declaration

    Swift

    public mutating func padStart(_ length: Int, with string: String = " ")

    Parameters

    length

    The target length to pad.

    string

    Pad string. Default is “ ”.

  • SwifterSwift: Returns a string by padding to fit the length parameter size with another string in the start.

    hue.paddingStart(10) -> hue hue.paddingStart(10, with: br) -> brbrbrbhue

    Declaration

    Swift

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

    Parameters

    length

    The target length to pad.

    string

    Pad string. Default is “ ”.

    Return Value

    The string with the padding on the start.

  • SwifterSwift: Pad string to fit the length parameter size with another string in the start.

    hue.padEnd(10) -> hue hue.padEnd(10, with: br) -> huebrbrbrb

    Declaration

    Swift

    public mutating func padEnd(_ length: Int, with string: String = " ")

    Parameters

    length

    The target length to pad.

    string

    Pad string. Default is “ ”.

  • SwifterSwift: Returns a string by padding to fit the length parameter size with another string in the end.

    hue.paddingEnd(10) -> hue hue.paddingEnd(10, with: br) -> huebrbrbrb

    Declaration

    Swift

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

    Parameters

    length

    The target length to pad.

    string

    Pad string. Default is “ ”.

    Return Value

    The string with the padding on the end.

  • SwifterSwift: Removes given prefix from the string.

    Hello, World!.removingPrefix(Hello,) -> World!

    Declaration

    Swift

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

    Parameters

    prefix

    Prefix to remove from the string.

    Return Value

    The string after prefix removing.

  • SwifterSwift: Removes given suffix from the string.

    Hello, World!.removingSuffix(, World!) -> Hello

    Declaration

    Swift

    public func removingSuffix(_ suffix: String) -> String

    Parameters

    suffix

    Suffix to remove from the string.

    Return Value

    The string after suffix removing.

  • SwifterSwift: Create a new string from a base64 string (if applicable).

    String(base64: "SGVsbG8gV29ybGQh") = "Hello World!"
    String(base64: "hello") = nil
    

    Declaration

    Swift

    public init?(base64: String)

    Parameters

    base64

    base64 string.

  • SwifterSwift: Create a new random string of given length.

    String(randomOfLength: 10) -> "gY8r3MHvlQ"
    

    Declaration

    Swift

    public init(randomOfLength length: Int)

    Parameters

    length

    number of characters in string.

  • SwifterSwift: Bold string.

    Declaration

    Swift

    public var bold: NSAttributedString { get }
  • SwifterSwift: Underlined string

    Declaration

    Swift

    public var underline: NSAttributedString { get }
  • SwifterSwift: Strikethrough string.

    Declaration

    Swift

    public var strikethrough: NSAttributedString { get }
  • SwifterSwift: Italic string.

    Declaration

    Swift

    public var italic: NSAttributedString { get }
  • SwifterSwift: Add color to string.

    Declaration

    Swift

    public func colored(with color: UIColor) -> NSAttributedString

    Parameters

    color

    text color.

    Return Value

    a NSAttributedString versions of string colored with given color.

  • SwifterSwift: Add color to string.

  • SwifterSwift: Repeat string multiple times.

       'bar' * 3 -> "barbarbar"
    

    Declaration

    Swift

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

    Parameters

    lhs

    string to repeat.

    rhs

    number of times to repeat character.

    Return Value

    new string with given string repeated n times.

  • SwifterSwift: Repeat string multiple times.

       3 * 'bar' -> "barbarbar"
    

    Declaration

    Swift

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

    Parameters

    lhs

    number of times to repeat character.

    rhs

    string to repeat.

    Return Value

    new string with given string repeated n times.

  • SwifterSwift: NSString from a string.

    Declaration

    Swift

    public var nsString: NSString { get }
  • SwifterSwift: NSString lastPathComponent.

    Declaration

    Swift

    public var lastPathComponent: String { get }
  • SwifterSwift: NSString pathExtension.

    Declaration

    Swift

    public var pathExtension: String { get }
  • SwifterSwift: NSString deletingLastPathComponent.

    Declaration

    Swift

    public var deletingLastPathComponent: String { get }
  • SwifterSwift: NSString deletingPathExtension.

    Declaration

    Swift

    public var deletingPathExtension: String { get }
  • SwifterSwift: NSString pathComponents.

    Declaration

    Swift

    public var pathComponents: [String] { get }
  • SwifterSwift: NSString appendingPathComponent(str: String)

    Declaration

    Swift

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

    Parameters

    str

    the path component to append to the receiver.

    Return Value

    a new string made by appending aString to the receiver, preceded if necessary by a path separator.

  • SwifterSwift: NSString appendingPathExtension(str: String)

    Declaration

    Swift

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

    Parameters

    str

    The extension to append to the receiver.

    Return Value

    a new string made by appending to the receiver an extension separator followed by ext (if applicable).