String
public extension String
-
SwifterSwift: String decoded from base64 (if applicable).
"SGVsbG8gV29ybGQh".base64Decoded = Optional("Hello World!")
Declaration
Swift
var base64Decoded: String? { get }
-
SwifterSwift: String encoded in base64 (if applicable).
"Hello World!".base64Encoded -> Optional("SGVsbG8gV29ybGQh")
Declaration
Swift
var base64Encoded: String? { get }
-
SwifterSwift: Array of characters of a string.
Declaration
Swift
var charactersArray: [Character] { get }
-
SwifterSwift: CamelCase of string.
"sOme vAriable naMe".camelCased -> "someVariableName"
Declaration
Swift
var camelCased: String { get }
-
SwifterSwift: Check if string contains one or more emojis.
"Hello 😀".containEmoji -> true
Declaration
Swift
var containEmoji: Bool { get }
-
SwifterSwift: First character of string (if applicable).
"Hello".firstCharacterAsString -> Optional("H") "".firstCharacterAsString -> nil
Declaration
Swift
var firstCharacterAsString: String? { get }
-
SwifterSwift: Check if string contains one or more letters.
"123abc".hasLetters -> true "123".hasLetters -> false
Declaration
Swift
var hasLetters: Bool { get }
-
SwifterSwift: Check if string contains one or more numbers.
"abcd".hasNumbers -> false "123abc".hasNumbers -> true
Declaration
Swift
var hasNumbers: Bool { get }
-
SwifterSwift: Check if string contains only letters.
"abc".isAlphabetic -> true "123abc".isAlphabetic -> false
Declaration
Swift
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
var isAlphaNumeric: Bool { get }
-
SwifterSwift: Check if string is palindrome.
"abcdcba".isPalindrome -> true "Mom".isPalindrome -> true "A man a plan a canal, Panama!".isPalindrome -> true "Mama".isPalindrome -> false
Declaration
Swift
var isPalindrome: Bool { get }
-
SwifterSwift: Check if string is valid email format.
Note
Note that this property does not validate the email address against an email server. It merely attempts to determine whether its format is suitable for an email address.
“[email protected]”.isValidEmail -> true
Declaration
Swift
var isValidEmail: Bool { get }
-
SwifterSwift: Check if string is a valid URL.
"https://google.com".isValidUrl -> true
Declaration
Swift
var isValidUrl: Bool { get }
-
SwifterSwift: Check if string is a valid schemed URL.
"https://google.com".isValidSchemedUrl -> true "google.com".isValidSchemedUrl -> false
Declaration
Swift
var isValidSchemedUrl: Bool { get }
-
SwifterSwift: Check if string is a valid https URL.
"https://google.com".isValidHttpsUrl -> true
Declaration
Swift
var isValidHttpsUrl: Bool { get }
-
SwifterSwift: Check if string is a valid http URL.
"http://google.com".isValidHttpUrl -> true
Declaration
Swift
var isValidHttpUrl: Bool { get }
-
SwifterSwift: Check if string is a valid file URL.
"file://Documents/file.txt".isValidFileUrl -> true
Declaration
Swift
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
var isNumeric: Bool { get }
-
SwifterSwift: Check if string only contains digits.
"123".isDigits -> true "1.3".isDigits -> false "abc".isDigits -> false
Declaration
Swift
var isDigits: Bool { get }
-
SwifterSwift: Last character of string (if applicable).
"Hello".lastCharacterAsString -> Optional("o") "".lastCharacterAsString -> nil
Declaration
Swift
var lastCharacterAsString: String? { get }
-
SwifterSwift: Latinized string.
"Hèllö Wórld!".latinized -> "Hello World!"
Declaration
Swift
var latinized: String { get }
-
SwifterSwift: Bool value from string (if applicable).
"1".bool -> true "False".bool -> false "Hello".bool = nil
Declaration
Swift
var bool: Bool? { get }
-
SwifterSwift: Date object from “yyyy-MM-dd” formatted string.
"2007-06-29".date -> Optional(Date)
Declaration
Swift
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
var dateTime: Date? { get }
-
SwifterSwift: Integer value from string (if applicable).
"101".int -> 101
Declaration
Swift
var int: Int? { get }
-
SwifterSwift: Lorem ipsum string of given length.
Declaration
Swift
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.
-
SwifterSwift: URL from string (if applicable).
"https://google.com".url -> URL(string: "https://google.com") "not url".url -> nil
Declaration
Swift
var url: URL? { get }
-
SwifterSwift: String with no spaces or new lines in beginning and end.
" hello \n".trimmed -> "hello"
Declaration
Swift
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
var urlDecoded: String { get }
-
SwifterSwift: URL escaped string.
"it's easy to encode strings".urlEncoded -> "it's%20easy%20to%20encode%20strings"
Declaration
Swift
var urlEncoded: String { get }
-
SwifterSwift: String without spaces and new lines.
" \n Swifter \n Swift ".withoutSpacesAndNewLines -> "SwifterSwift"
Declaration
Swift
var withoutSpacesAndNewLines: String { get }
-
SwifterSwift: Check if the given string contains only white spaces
Declaration
Swift
var isWhitespace: Bool { get }
-
SwifterSwift: Check if the given string spelled correctly
Declaration
Swift
var isSpelledCorrectly: Bool { get }
-
SwifterSwift: Float value from string (if applicable).
Declaration
Swift
func float(locale: Locale = .current) -> Float?
Parameters
locale
Locale (default is Locale.current)
Return Value
Optional Float value from given string.
-
SwifterSwift: Double value from string (if applicable).
Declaration
Swift
func double(locale: Locale = .current) -> Double?
Parameters
locale
Locale (default is Locale.current)
Return Value
Optional Double value from given string.
-
SwifterSwift: CGFloat value from string (if applicable).
Declaration
Swift
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
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
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
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
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
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
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
func toSlug() -> String
Return Value
The string in slug format.
-
SwifterSwift: Safely subscript string with index.
"Hello World!"[safe: 3] -> "l" "Hello World!"[safe: 20] -> nil
Declaration
Swift
subscript(safe index: Int) -> Character? { get }
Parameters
index
index.
-
SwifterSwift: Safely subscript string within a given range.
"Hello World!"[safe: 6..<11] -> "World" "Hello World!"[safe: 21..<110] -> nil "Hello World!"[safe: 6...11] -> "World!" "Hello World!"[safe: 21...110] -> nil
Declaration
Swift
subscript<R>(safe range: R) -> String? where R : RangeExpression, R.Bound == Int { get }
Parameters
range
Range expression.
-
SwifterSwift: Copy string to global pasteboard.
"SomeText".copyToPasteboard() // copies "SomeText" to pasteboard
Declaration
Swift
func copyToPasteboard()
-
SwifterSwift: Converts string format to CamelCase.
var str = "sOme vaRiabLe Name" str.camelize() print(str) // prints "someVariableName"
Declaration
Swift
@discardableResult mutating mutating func camelize() -> String
-
SwifterSwift: First character of string uppercased(if applicable) while keeping the original string.
"hello world".firstCharacterUppercased() -> "Hello world" "".firstCharacterUppercased() -> ""
Declaration
Swift
mutating mutating func firstCharacterUppercased()
-
SwifterSwift: Check if string contains only unique characters.
Declaration
Swift
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
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
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
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
@discardableResult mutating mutating func latinize() -> String
-
SwifterSwift: Random string of given length.
String.random(ofLength: 18) -> "u7MMZYvGo9obcOcPj8"
Declaration
Swift
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
@discardableResult mutating mutating func reverse() -> String
-
SwifterSwift: Sliced string from a start index with length.
"Hello World".slicing(from: 6, length: 5) -> "World"
Declaration
Swift
func slicing(from index: Int, length: Int) -> String?
Parameters
index
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
@discardableResult mutating mutating func slice(from index: Int, length: Int) -> String
Parameters
index
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
@discardableResult mutating mutating func slice(from start: Int, to end: Int) -> String
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
@discardableResult mutating mutating func slice(at index: Int) -> String
Parameters
index
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
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
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
@discardableResult mutating mutating func trim() -> String
-
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
@discardableResult mutating mutating func truncate(toLength length: Int, trailing: String? = "...") -> 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
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
@discardableResult mutating mutating func urlDecode() -> String
-
SwifterSwift: Escape string.
var str = "it's easy to encode strings" str.urlEncode() print(str) // prints "it's%20easy%20to%20encode%20strings"
Declaration
Swift
@discardableResult mutating mutating func urlEncode() -> String
-
SwifterSwift: Verify if string matches the regex pattern.
Declaration
Swift
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
@discardableResult mutating mutating func padStart(_ length: Int, with string: 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
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
@discardableResult mutating mutating func padEnd(_ length: Int, with string: 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
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
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
func removingSuffix(_ suffix: String) -> String
Parameters
suffix
Suffix to remove from the string.
Return Value
The string after suffix removing.
-
SwifterSwift: Adds prefix to the string.
"www.apple.com".withPrefix("https://") -> "https://www.apple.com"
Declaration
Swift
func withPrefix(_ prefix: String) -> String
Parameters
prefix
Prefix to add to the string.
Return Value
The string with the prefix prepended.
-
SwifterSwift: Create a new string from a base64 string (if applicable).
String(base64: "SGVsbG8gV29ybGQh") = "Hello World!" String(base64: "hello") = nil
Declaration
Swift
init?(base64: String)
Parameters
base64
base64 string.
-
SwifterSwift: Create a new random string of given length.
String(randomOfLength: 10) -> "gY8r3MHvlQ"
Declaration
Swift
init(randomOfLength length: Int)
Parameters
length
number of characters in string.
-
SwifterSwift: Bold string.
Declaration
Swift
var bold: NSAttributedString { get }
-
SwifterSwift: Underlined string
Declaration
Swift
var underline: NSAttributedString { get }
-
SwifterSwift: Strikethrough string.
Declaration
Swift
var strikethrough: NSAttributedString { get }
-
SwifterSwift: Italic string.
Declaration
Swift
var italic: NSAttributedString { get }
-
SwifterSwift: Add color to string.
Declaration
Swift
func colored(with color: Color) -> NSAttributedString
Parameters
color
text color.
Return Value
a NSAttributedString versions of string colored with given color.
-
SwifterSwift: Repeat string multiple times.
'bar' * 3 -> "barbarbar"
Declaration
Swift
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
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
var nsString: NSString { get }
-
SwifterSwift: NSString lastPathComponent.
Declaration
Swift
var lastPathComponent: String { get }
-
SwifterSwift: NSString pathExtension.
Declaration
Swift
var pathExtension: String { get }
-
SwifterSwift: NSString deletingLastPathComponent.
Declaration
Swift
var deletingLastPathComponent: String { get }
-
SwifterSwift: NSString deletingPathExtension.
Declaration
Swift
var deletingPathExtension: String { get }
-
SwifterSwift: NSString pathComponents.
Declaration
Swift
var pathComponents: [String] { get }
-
SwifterSwift: NSString appendingPathComponent(str: String)
Note
This method only works with file paths (not, for example, string representations of URLs. See NSString appendingPathComponent(_:)Declaration
Swift
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
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).