String Extensions

Omar Albeik edited this page Jan 6, 2017 · 11 revisions

String extensions

Properties:

Name Description
base64Decoded String decoded from base64 (if applicable).
base64Encoded String encoded in base64 (if applicable).
camelCased CamelCase of string.
containEmoji Check if string contains one or more emojis.
firstCharacter First character of string (if applicable).
hasLetters Check if string contains one or more letters.
hasNumbers Check if string contains one or more numbers.
isAlphabetic Check if string contains only letters.
isAlphaNumeric Check if string contains at least one letter and one number.
isEmail Check if string is valid email format.
isHttpsUrl Check if string is https URL.
isHttpUrl Check if string is http URL.
isNumeric Check if string contains only numbers.
lastCharacter Last character of string (if applicable).
latinized Latinized string.
lines Array of strings separated by new lines.
mostCommonCharacter The most common character in string.
reversed Reversed string.
bool Bool value from string (if applicable).
date Date object from "yyyy-MM-dd" formatted string
dateTime Date object from "yyyy-MM-dd HH:mm:ss" formatted string.
double Double value from string (if applicable).
float Float value from string (if applicable).
float32 Float32 value from string (if applicable).
float64 Float64 value from string (if applicable).
int Integer value from string (if applicable).
int16 Int16 value from string (if applicable).
int32 Int32 value from string (if applicable).
int64 Int64 value from string (if applicable).
int8 Int8 value from string (if applicable).
url URL from string (if applicable).
trimmed String with no spaces or new lines in beginning and end.
unicodeArray Array with unicodes for all characters in a string.
urlDecoded Readable string from a URL string.
urlEncoded URL escaped string.
withoutSpacesAndNewLines String without spaces and new lines.
bold Bold string.
underline Underlined string
strikethrough Strikethrough string.
italic Italic string.
nsString NSString from a string
lastPathComponent NSString lastPathComponent
pathExtension NSString pathExtension
deletingLastPathComponent NSString deletingLastPathComponent
deletingPathExtension NSString deletingPathExtension
pathComponents NSString pathComponents

Methods:

Name Return Description
copyToPasteboard() -- Copy string to global pasteboard.
camelize() -- Converts string format to CamelCase.
contain(_ string: String, caseSensitive: Bool = true) Bool Check if string contains one or more instance of substring.
count(of string: String, caseSensitive: Bool = true) Int Count of substring in string.
end(with suffix: String, caseSensitive: Bool = true) Bool Check if string ends with substring.
firstIndex(of string: String) Int? First index of substring in string.
latinize() -- Latinize string.
random(ofLength: Int) String Random string of given length.
replacing(_ substring: String, with newString: String) String String by replacing part of string with another string.
reverse() -- Reverse string.
slicing(from i: Int, length: Int) String? Sliced string from a start index with length.
slice(from i: Int, length: Int) -- Slice given string from a start index with length (if applicable).
slicing(from start: Int, to end: Int) String? Sliced string from a start index to an end index.
slice(from start: Int, to end: Int) -- Slice given string from a start index to an end index (if applicable).
slicing(at i: Int) String? Sliced string from a start index.
slice(at i: Int) -- Slice given string from a start index (if applicable).
splited(by separator: Character) [String] Array of strings separated by given string.
start(with prefix: String, caseSensitive: Bool = true) Bool Check if string starts with substring.
subscript(i: Int) String? Subscript string with index.
subscript(range: CountableRange) String? Subscript string within a half-open range.
subscript(range: ClosedRange) String? Subscript string within a closed range.
date(withFormat format: String) Date? Date object from string of date format.
trim() -- Removes spaces and new lines in beginning and end of string.
truncate(toLength: Int, trailing: String? = "...") -- Truncate string (cut it to a given number of characters).
truncated(toLength: Int, trailing: String? = "...") String Truncated string (limited to a given number of characters).
urlDecode() -- Convert URL string to readable string.
urlEncode() -- Escape string.
colored(with color: UIColor) NSAttributedString Add color to string.
appendingPathComponent(_ str: String) String NSString appendingPathComponent(str: String)
appendingPathExtension(_ str: String) String? NSString appendingPathExtension(str: String) (if applicable).

Operators:

Name Return Description
Int * String String Repeat string multiple times.
String * Int String Repeat string multiple times.

Initializers:

Name Description
init?(base64: String) Create a new string from a base64 string (if applicable).