String
struct String
-
Mechanica
Returns a
newstring decoded from base64.Declaration
Swift
public var base64Decoded: String? { get } -
Mechanica
Returns a
newstring encoded in base64.Declaration
Swift
public var base64Encoded: String? { get } -
Mechanica
Returns a Bool value by parsing
self.Example:
"👍🏼".bool -> true "0".bool -> falseDeclaration
Swift
public var bool: Bool? { get } -
Mechanica
Produces a
newstring with the first character of the first word changed to the corresponding uppercase value.Declaration
Swift
public func capitalizedFirstCharacter() -> String -
Mechanica
Condenses all white spaces repetitions in a single white space. White space at the beginning or ending of the
Stringare trimmed out.Example:
let aString = "test too many spaces" aString.removeExcessiveSpaces -> test too many spacesDeclaration
Swift
public func condensingExcessiveSpaces() -> StringReturn Value
A
newstring 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
Stringare trimmed out.Declaration
Swift
public func condensingExcessiveSpacesAndNewlines() -> StringReturn Value
A
newstring where all white spaces and new lines repetitions are replaced with a single white space. -
Mechanica
Checks if
selfcontains aString.Declaration
Swift
public func contains(_ string: String, caseSensitive: Bool) -> BoolParameters
stringString to match.
caseSensitiveSearch option: true for case-sensitive, false for case-insensitive. (if true this function is equivalent to
self.contains(...))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) -> BoolParameters
characterSetThe
CharacterSetused to test the string.Return Value
true if all the characters in the string belong to the
CharacterSet, otherwise false. -
Mechanica
Produces a
newstring with the first character of the first word changed to the corresponding uppercase value.Declaration
Swift
public func decapitalizedFirstCharacter() -> String -
Mechanica
Returns true if
selfends with a given suffix.Declaration
Swift
public func ends(with suffix: String, caseSensitive: Bool = true) -> Bool -
Mechanica
Makes sure that we always have a semantic version in the form MAJOR.MINOR.PATCH
Declaration
Swift
public func ensureSemanticVersionCorrectness() -> String -
Mechanica
Returns a list containing the first character of each word contained in
self.Declaration
Swift
public func firstCharacterOfEachWord() -> [String] -
Mechanica
Returns true if the
Stringcontains one or more letters.Declaration
Swift
public var hasLetters: Bool { get } -
Mechanica
Returns true if the
Stringcontains one or more numbers.Declaration
Swift
public var hasNumbers: Bool { get } -
Mechanica
Returns true if the
Stringcontains only letters.Declaration
Swift
public var isAlphabetic: Bool { get } -
Mechanica
Returns true if the
Stringcontains only letters, number.Declaration
Swift
public var isAlphaNumeric: Bool { get } -
Mechanica
Checks if the
Stringis blank (a string that is either empty or contains only space/newline characters).Declaration
Swift
public var isBlank: Bool { get } -
Mechanica
Returns
trueifselfis 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 { get } -
Mechanica
Checks if the
Stringcontains only numbers.Declaration
Swift
public var isNumeric: Bool { get } -
Mechanica
Checks if
selfis a semantic version with a value equal to a givenversion.Declaration
Swift
public func isSemanticVersionEqual(to version: String) -> Bool -
Mechanica
Checks if
selfis a semantic version with a value greater than givenversion.Declaration
Swift
public func isSemanticVersionGreater(than version: String) -> Bool -
Mechanica
Checks if
selfis a semantic version with a value greater or equal to a givenversion.Declaration
Swift
public func isSemanticVersionGreaterOrEqual(to version: String) -> Bool -
Mechanica
Checks if
selfis a semantic version with a value lesser than a givenversion.Declaration
Swift
public func isSemanticVersionLesser(than version: String) -> Bool -
Mechanica
Checks if
selfis a semantic version with a value lesser or equal to a givenversion.Declaration
Swift
public func isSemanticVersionLesserOrEqual(to version: String) -> Bool -
Mechanica
Returns true if the
Stringis a valid email.Declaration
Swift
public var isValidEmail: Bool { get } -
Mechanica
Checks if string is a valid file URL.
Example:
"file://Documents/file.txt".isValidFileUrl -> trueDeclaration
Swift
public var isValidFileUrl: Bool { get } -
Mechanica
Checks if string is a valid http URL.
Example:
"http://tinrobots.org".isValidHttpUrl -> trueDeclaration
Swift
public var isValidHttpUrl: Bool { get } -
Mechanica
Checks if string is a valid https URL.
Example:
"https://tinrobots.org".isValidHttpsUrl -> trueDeclaration
Swift
public var isValidHttpsUrl: Bool { get } -
Mechanica
Check if the
Stringis a valid schemed URL.Example:
"https://tinrobots.org".isValidSchemedUrl -> true "tinrobots.org".isValidSchemedUrl -> falseDeclaration
Swift
public var isValidSchemedUrl: Bool { get } -
Mechanica
Checks if string is a valid URL.
Example:
"https://tinrobots.org".isValidUrl -> trueDeclaration
Swift
public var isValidUrl: Bool { get } -
Mechanica
Generates a
newrandomString.Declaration
Swift
public static func random(length: UInt32 = 8, characters base: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> StringParameters
lengthThe length (defaults to 8) of the generated string.
baseThe characters to be used to generate the new string (the default characters are the alphanumeric ones).
Return Value
A
newrandom alphanumericString. -
Mechanica
Generates a
newrandomString.Declaration
Swift
public static func random(length between: CountableClosedRange<UInt32>, characters base: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> StringParameters
betweenThe range of possible lengths for the new string.
baseThe characters to be used to generate the new string (the default characters are the alphanumeric ones).
Return Value
A
newrandom alphanumericString. -
Mechanica
Returns a
newString stripped of all accents and diacritics.Example:
let string = "äöüÄÖÜ" -> ÄÖÜ let stripped = string.removingAccentsOrDiacritics() -> aouAOUDeclaration
Swift
@available(iOS 9.0, OSX 10.11, tvOS 9.0, watchOS 2.0, *) public func removingAccentsOrDiacritics() -> String -
Mechanica*
Remove the characters in the given set.
Declaration
Swift
public mutating func removeCharacters(in set: CharacterSet) -
Mechanica*
Returns a new
Stringremoving the characters in the given set.Declaration
Swift
public func removingCharacters(in set: CharacterSet) -> String -
Mechanica
Returns a
newstring 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) -> StringParameters
targettarget string
replacementreplacement string
caseSensitive*true*(default) for a case-sensitive replacemente -
Mechanica
Returns a
newstring in which the characters in a specifiedCountableClosedRangerange 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
newstring in which the characters in a specifiedCountableRangerange of the String are replaced by a given string.Declaration
Swift
public func replacingCharacters(in range: CountableRange<Int>, with replacement: String) -> String -
Mechanica
Returns a
newstring in which the first occurrence of a target is replaced by another given string.Declaration
Swift
public func replacingFirstOccurrence(of string: String, with replacement: String) -> String -
Mechanica
If
selfis a semantic version, returns a tuple with major, minor and patch components.Declaration
Swift
public var semanticVersion: (Int, Int, Int) { get } -
Mechanica
Returns true if
selfstarts with a given prefix.Declaration
Swift
public func starts(with prefix: String, caseSensitive: Bool = true) -> Bool -
Mechanica
Removes spaces and new lines from both ends of `self.
Declaration
Swift
public mutating func trim() -
Mechanica*
Returns a
newString made by removing spaces and new lines from both ends.Declaration
Swift
public func trimmed() -> String -
Mechanica*
Strips the specified characters from the end of
self.Declaration
Swift
public func trimmedEnd(characterSet set: CharacterSet = .whitespacesAndNewlines) -> StringParameters
setcharacters to strip
Return Value
stripped string
-
Mechanica*
Strips the specified characters from the beginning of
self.Declaration
Swift
public func trimmedStart(characterSet set: CharacterSet = .whitespacesAndNewlines) -> StringParameters
setcharacters to strip
Return Value
stripped string
-
Mechanica
Returns a percent-escaped string following RFC 3986 for a query string key or value.
Declaration
Swift
public var urlEscaped: String? { get }
-
Mechanica*
Returns a substring for a given
NSRange.Declaration
Swift
public subscript(nsRange: NSRange) -> Substring? { get }Parameters
rangeNSRange.
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>? { get }Parameters
substringsubstring
Return Value
The range of the first occurrence or nil.
-
Mechanica
Produces a camel cased version of the
String.Example:
let string = "HelloWorld" string.camelCased() -> "helloWorld"Declaration
Swift
public func camelCased() -> StringReturn Value
A camel cased copy of the
String. -
Mechanica
Produces the kebab cased version of the
String.Example:
let string = "Hello World" string.kebabCased() -> "-Hello-World-"Declaration
Swift
public func kebabCased() -> StringReturn Value
The kebab cased copy of the
String. -
Mechanica
Produces a pascal cased version of the
String.Example:
let string = "HELLO WORLD" string.pascalCased() -> "HelloWorld"Declaration
Swift
public func pascalCased() -> StringReturn Value
A pascal cased copy of the
String. -
Mechanica
Produces the slug version of the
String.Example:
let string = "Hello World" string.slugCased() -> "Hello-World"Declaration
Swift
public func slugCased() -> StringReturn Value
The slug copy of the
String. -
Mechanica
Produces the snake cased version of the
String.Example:
let string = "hello world" string.snakeCased() -> "hello_world"Declaration
Swift
public func snakeCased() -> StringReturn Value
The slug copy of the
String. -
Mechanica
Produces the swap cased version of the
String.Example:
let string = "Hello World" string.swapCased() -> "hELLO wORLD"Declaration
Swift
public func swapCased() -> StringReturn Value
The swap cased copy of the
String.
-
Mechanica
Returns a new string made by appending to the receiver a given string.
Declaration
Swift
public func appendingPathComponent(_ path: String) -> String -
Mechanica
Returns a new string made by appending to the receiver an extension separator followed by a given extension.
Declaration
Swift
public func appendingPathExtension(_ ext: String) -> String? -
Mechanica
Returns a
newstring made by deleting the last path component from the receiver, along with any final path separator.Note
This method only works with file paths (not, for example, string representations of URLs).Declaration
Swift
public var deletingLastPathComponent: String { get } -
Mechanica
Returns a
newstring made by deleting the extension (if any, and only the last) from the receiver.Declaration
Swift
public var deletingPathExtension: String { get } -
Mechanica
Returns the last path component of the receiver.
Note
This method only works with file paths (not, for example, string representations of URLs).Declaration
Swift
public var lastPathComponent: String { get } -
Mechanica
Returns the file-system path components of the receiver.
Declaration
Swift
public var pathComponents: [String] { get } -
Mechanica
Return the path extension, if any, of the string as interpreted as a path.
Note
This method only works with file paths (not, for example, string representations of URLs).Declaration
Swift
public var pathExtension: String { get }
-
Mechanica
Declaration
Swift
public func firstMatch(for pattern: String, options: NSRegularExpression.Options = []) -> String?Parameters
patterna regular expression pattern.
optionsa list of
NSRegularExpression.Options.Return Value
The first match of the regular expression within
self. -
Mechanica
Declaration
Swift
public func firstRange(matching pattern: String, options: NSRegularExpression.Options = []) -> Range<String.Index>?Parameters
patterna regular expression pattern.
optionsa list of
NSRegularExpression.Options.Return Value
The first match’s range of the regular expression within
self. -
Mechanica
Declaration
Swift
public func matches(for pattern: String, options: NSRegularExpression.Options = []) -> [String]Parameters
patterna regular expression pattern.
optionsa list of
NSRegularExpression.Options.Return Value
A list of matches of the regular expression within
self. -
Mechanica
Declaration
Swift
public func ranges(matching pattern: String, options: NSRegularExpression.Options = []) -> [Range<String.Index>]Parameters
patterna regular expression pattern.
optionsa list of
NSRegularExpression.Options.Return Value
A list of matched ranges for
self.
-
Creates a string containing the given ‘StaticString’.
Declaration
Swift
public init(staticString: StaticString)Parameters
staticStringThe ‘StaticString’ to convert to a string.
-
Mechanica
Returns a
newstring containing the first character of theString.Declaration
Swift
public var first: String { get } -
Mechanica
Checks if all of the characters in a string are all the same.
Declaration
Swift
public var isHomogeneous: Bool { get } -
Mechanica
Returns true if all the characters are lowercased.
Declaration
Swift
public var isLowercase: Bool { get } -
Mechanica
Returns true, if all characters are uppercased. Otherwise, false.
Declaration
Swift
public var isUppercase: Bool { get } -
Mechanica
Returns a
newstring containing the last character of theString.Declaration
Swift
public var last: String { get } -
Mechanica
Returns the length of the
String.Declaration
Swift
public var length: Int { get } -
Mechanica
Returns a
newString with the center-padded version ofselfif 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.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 = " ") -> StringParameters
lengthThe final length of your string. If the provided length is less than or equal to the original string, the original string is returned.
tokenThe string used to pad the String (defaults to a white space).
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 = " ")Parameters
lengthThe final length of your string. If the provided length is less than or equal to the original string, the original string is returned. If the the sum-total of characters added is odd, the left side of the string will have one less instance of the token.
tokenThe string used to pad the String (defaults to a white space).
-
Mechanica
Returns a
newString with the left-padded version ofselfif 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.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 = " ") -> StringParameters
lengthThe final length of your string.
tokenThe string used to pad the String (defaults to a white space).
Return Value
The left-padded copy of the string.
-
Mechanica
Pads
selfon the left side if it’s shorter thanlengthusing atoken. 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 = " ")Parameters
lengthThe final length of your string.
tokenThe string used to pad the String (defaults to a white space).
-
Mechanica
Returns a
newString with the right-padded version ofselfif 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.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 = " ") -> StringParameters
lengthThe final length of your string.
tokenThe string used to pad the String (defaults to a white space).
Return Value
The right-padded copy of the string.
-
Mechanica
Pads
selfon the right side if it’s shorter thanlengthusing atoken. 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 = " ")Parameters
lengthThe final length of your string.
tokenThe string used to pad the String (defaults to a white space).
-
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) -> StringParameters
maxLengthThe maximum number of elements to return. maxLength must be greater than or equal to zero.
-
Mechanica
Returns a new
Stringcontaining the characters of the String from the one at a given position to the end.Example:
"hello".droppingPrefix(upToPosition: 1) -> "ello" "hello".droppingPrefix(upToPosition: 1) -> ""Declaration
Swift
public func droppingPrefix(upToPosition: Int = 1) -> StringParameters
upToPositionposition (included) up to which remove the prefix.
-
Mechanica
Returns a new
Stringremoving the spcified prefix (if the string has it).Example:
"hello".droppingPrefix("hel") -> "lo"Declaration
Swift
public func droppingPrefix(_ prefix: String) -> StringParameters
prefixprefix to be removed.
-
Mechanica
Returns a new
Stringcontaining the characters of the String up to, but not including, the one at a given position.Example:
"hello".droppingSuffix(fromPosition: 1) -> "hell" "hello".droppingSuffix(fromPosition: 10) -> ""Declaration
Swift
public func droppingSuffix(fromPosition: Int = 1) -> StringParameters
fromPositionposition (included) from which remove the suffix
-
Mechanica
Returns a new
Stringremoving the spcified suffix (if the string has it).Example:
"hello".droppingSuffix("0") -> "hell"Declaration
Swift
public func droppingSuffix(_ suffix: String) -> StringParameters
prefixprefix to be removed.
-
Mechanica
Reverse
self.Declaration
Swift
public mutating func reverse() -
Mechanica*
Returns a slice, up to maxLength in length, containing the final elements of
String.Warning
If maxLength exceedsStringcharacter count, the result contains all the elements ofString.Declaration
Swift
public func suffix(maxLength: Int) -> StringParameters
maxLengthThe maximum number of elements to return. maxLength must be greater than or equal to zero.
-
Mechanica*
Truncates the
Stringto 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? = "…") -> StringParameters
lengthnumber of characters after which the
Stringis truncatedtrailingoptional trailing string
-
Mechanica
Gets the character at the specified index as String.
Example:
let myString = "Hello world" myString[0] -> "H" myString[3] -> "l" myString[10] -> "d" myString[11] -> nil myString[-1] -> nilDeclaration
Swift
public subscript(index: Int) -> Character? { get }Parameters
indexindex Position of the character to get
Return Value
Character as String or nil if the index is out of bounds
-
Mechanica
Returns a substring for the given range.
Example:
let myString = "Hello world" myString[0..<0] -> "" myString[0..<1] -> "Hello worl" myString[0..<10] -> "Hello world" myString[0..<11] -> nil myString[11..<14] -> nil myString[-1..<11] -> nilDeclaration
Swift
public subscript(range: CountableRange<Int>) -> Substring? { get }Parameters
rangea
CountableRangerange. -
Mechanica
Returns a substring for the given range.
Example:
let myString = "Hello world" myString[0...0] myString[0...1] myString[0...10] myString[0...11] myString[11...14] myString[-1...11]Declaration
Swift
public subscript(range: CountableClosedRange<Int>) -> Substring? { get }Parameters
rangea
CountableClosedRangerange. -
Mechanica
Returns a substring for the given range.
Example:
let myString = "Hello world" myString[0...0] -> "H" myString[0...1] -> "He" myString[0...10] -> "Hello world" myString[0...11] -> nil myString[11...14] -> nil myString[-1...11] -> nilDeclaration
Swift
public subscript(range: PartialRangeUpTo<Int>) -> Substring? { get }Parameters
rangea
PartialRangeUpTorange. -
Mechanica
Declaration
Swift
public subscript(range: PartialRangeThrough<Int>) -> Substring? { get }Parameters
rangea
PartialRangeThroughrange. Example:let myString =
Hello world
myString[..<0] -> “myString[..<1] ->
HmyString[..<10] ->
Hello worlmyString[..<11] ->
Hello world” myString[..<14] -> nil myString[..<(-1)] -> nil -
Mechanica
Returns a substring for the given range.
Declaration
Swift
public subscript(range: CountablePartialRangeFrom<Int>) -> Substring? { get }Parameters
rangea
CountablePartialRangeFromrange. Example:let myString =
Hello world
myString[0…] ->Hello world
myString[1…] ->ello world
myString[10…] ->d
myString[11…] -> nil myString[14…] -> nil myString[(-1)…] -> nil
-
Mechanica
Returns a substring for the given range. Creates a
newstring representing the given string repeated the specified number of times.Declaration
Swift
public static func * (lhs: String, rhs: Int) -> String -
Mechanica
Returns a substring for the given range. Creates a
newstring representing the given string repeated the specified number of times.Declaration
Swift
public static func * (lhs: Int, rhs: String) -> String
String Extension Reference