String

extension String
  • Converts self to an unsigned byte array.

    Declaration

    Swift

    public var bytes: [UInt8] { get }
  • Converts self to an NSMutableAttributedString.

    Declaration

    Swift

    public var attributed: NSMutableAttributedString { get }
  • ns

    Converts self to an NSString.

    Declaration

    Swift

    public var ns: NSString { get }
  • Converts string to camel-case.

    Examples:

    "os version".camelCasedString                 // "osVersion"
    "HelloWorld".camelCasedString                 // "helloWorld"
    "someword With Characters".camelCasedString   // "somewordWithCharacters"
    

    Declaration

    Swift

    public var camelCased: String { get }
  • The base64 encoded version of self. Credit: http://stackoverflow.com/a/29365954

    Declaration

    Swift

    public var base64Encoded: String? { get }
  • The decoded value of a base64 encoded string Credit: http://stackoverflow.com/a/29365954

    Declaration

    Swift

    public var base64Decoded: String? { get }
  • Returns true if every character within the string is a numeric character. Empty strings are considered non-numeric.

    Declaration

    Swift

    public var isNumeric: Bool { get }
  • Replaces all occurences of the pattern on self in-place.

    Examples:

    "hello".regexInPlace("[aeiou]", "*")      // "h*ll*"
    "hello".regexInPlace("([aeiou])", "<$1>") // "h<e>ll<o>"
    

    Declaration

    Swift

    public mutating func formRegex(_ pattern: String, _ replacement: String)
  • Returns a string containing replacements for all pattern matches.

    Examples:

    "hello".regex("[aeiou]", "*")      // "h*ll*"
    "hello".regex("([aeiou])", "<$1>") // "h<e>ll<o>"
    

    Declaration

    Swift

    public func regex(_ pattern: String, _ replacement: String) -> String
  • Replaces pattern-matched strings, operated upon by a closure, on self in-place.

    Example:

    "hello".regexInPlace(".") {
       let s = $0.unicodeScalars
       let v = s[s.startIndex].value
       return "\(v) "
    }   // "104 101 108 108 111 "
    

    Declaration

    Swift

    public mutating func formRegex(_ pattern: String, _ matches: (String) -> String)

    Parameters

    pattern

    The pattern to match against.

    matches

    The closure in which to handle matched strings.

  • Returns a string with pattern-matched strings, operated upon by a closure.

    Example:

    "hello".regex(".") {
       let s = $0.unicodeScalars
       let v = s[s.startIndex].value
       return "\(v) "
    }   // "104 101 108 108 111 "
    

    Declaration

    Swift

    public func regex(_ pattern: String, _ matches: (String) -> String) -> String

    Parameters

    pattern

    The pattern to match against.

    matches

    The closure in which to handle matched strings.

    Return Value

    String containing replacements for the matched pattern.

  • Substring at index

    Declaration

    Swift

    public subscript(i: Int) -> String { get }
  • Substring for range

    Declaration

    Swift

    public subscript(r: Range<Int>) -> String { get }
  • Substring for closed range

    Declaration

    Swift

    public subscript(r: ClosedRange<Int>) -> String { get }
  • Substring for countable partial range

    Declaration

    Swift

    public subscript(r: CountablePartialRangeFrom<Int>) -> String { get }
  • Substring for partial range through upper bound

    Declaration

    Swift

    public subscript(r: PartialRangeThrough<Int>) -> String { get }
  • Substring for partial range up to upper bound

    Declaration

    Swift

    public subscript(r: PartialRangeUpTo<Int>) -> String { get }
  • Truncates the string to length characters, optionally appending a trailing string. If the string is shorter than the required length, then this function is a non-op.

    Examples:

    "hello there".truncated(to: 5)                   // "hello"
    "hello there".truncated(to: 5, trailing: "...")  // "hello..."
    

    Declaration

    Swift

    public func truncated(to length: Int, trailing: String = "") -> String

    Parameters

    length

    The length of string required.

    trailing

    An optional addition to the end of the string (increasing length), such as ellipsis.

    Return Value

    The truncated string.

  • Undocumented

    Declaration

    Swift

    public mutating func truncate(to length: Int, trailing: String = "")
  • A bridge for invoking String.localizedStandardContainsString(), which is available in iOS 9 and later. If you need to support iOS versions prior to iOS 9, use compatibleStandardContainsString() as a means to bridge functionality. If you can support iOS 9 or greater only, use localizedStandardContainsString() directly.

    From Apple’s Swift 2.1 documentation:

    localizedStandardContainsString() is the most appropriate method for doing user-level string searches, similar to how searches are done generally in the system. The search is locale-aware, case and diacritic insensitive. The exact list of search options applied may change over time.

    Declaration

    Swift

    public func compatibleStandardContains(_ string: String) -> Bool

    Parameters

    string

    The string to determine if is contained by self.

    Return Value

    Returns true if self contains string, taking the current locale into account.

  • Convert an NSRange to a Range. There is still a mismatch between the regular expression libraries and NSString/String. This makes it easier to convert between the two. Using this allows complex strings (including emoji, regonial indicattors, etc.) to be manipulated without having to resort to NSString instances.

    Note that it may not always be possible to convert from an NSRange as they are not exactly the same.

    Taken from: http://stackoverflow.com/questions/25138339/nsrange-to-rangestring-index

    Declaration

    Swift

    public func range(from nsRange: NSRange) -> Range<String.Index>?

    Parameters

    nsRange

    The NSRange instance to covert to a Range.

    Return Value

    The Range, if it was possible to convert. Otherwise nil.

  • Convert a Range to an NSRange. There is still a mismatch between the regular expression libraries and NSString/String. This makes it easier to convert between the two. Using this allows complex strings (including emoji, regonial indicators, etc.) to be manipulated without having to resort to NSString instances.

    Taken from: http://stackoverflow.com/questions/25138339/nsrange-to-rangestring-index

    Declaration

    Swift

    public func nsRange(from range: Range<String.Index>) -> NSRange

    Parameters

    range

    The Range instance to conver to an NSRange.

    Return Value

    The NSRange converted from the input. This will always succeed.