Skip to content

String Extensions

Marco Quinten edited this page Feb 28, 2021 · 1 revision

Static Let-Bindings

/// An empty string.
static let empty: String = ""

/// A string containing a space character.
static let space: String = " "

/// A string containing a newline character.
static let newline: String = "\n"

/// A string containing a tab character.
static let tab: String = "\t"

Properties

/// Returns the string components, separated by newline characters.
var lines: [String]

/// A localized version of the receiver.
var localized: String

/// A URL-encoded version of the receiver.
var urlEncoded: String

/// A URL-decoded version of the receiver.
var urlDecoded: String

/// Returns an integer version of the receiver, if available.
///
/// - Remark: Int() does not support optional chaining.
var toInt: Int?

/// Returns a double version of the receiver, if available.
///
/// - Remark: Double() does not support optional chaining.
var toDouble: Double?

/// Extracts number of seconds from the receiver in a hh:mm:ss/mm:ss/ss format.
/// 
/// - Note:
///   The receiver is not restricted to having two digits for each component. Any amount is acceptable.
/// 
/// - Example:
///   ```
///   let time: String = "00:02:15"
///   let seconds = time.extractedSeconds // 135
///   ```
var extractedSeconds: Int

Non-Mutating Functions

/// Whether the receiver ends with the other string.
func ends(with string: String) -> Bool

/// A version of the receiver which has all occurrences of the other string removed.
func removingOccurances(of string: String) -> String

/// A version of the receiver which has all occurrences of the other string at the start removed.
func removingOccurancesAtStart(of string: String) -> String

/// A version of the receiver which has all occurrences of the other string at the end removed.
func removingOccurancesAtEnd(of string: String) -> String

/// Returns a localized version of the receiver.
func localized(with comment: String = .empty) -> String

/// Returns a double version of the receiver, if available.
/// 
/// - Note: This also respects region settings. (E.g. In some countries '.' and ',' are used the opposite way)
/// - Remark: Double() does not support optional chaining.
func formattedToDouble(separatesThousands: Bool = true, locale: Locale = .autoupdatingCurrent) -> Double?

/// Returns an actual index for an integer.
///
/// - Remark: The built-in index system is burdensome.
func indexAt(_ index: Int) -> String.Index

/// Returns an actual range for an NSRange.
/// 
/// - Remark: The built-in index system is burdensome.
func rangeFor(_ range: NSRange) -> Range<String.Index>

Mutating Functions

/// Capitalizes the receiver.
mutating func capitalize()

/// Lowercases the receiver.
mutating func lowercase()

/// Uppercases the receiver.
mutating func uppercase()

/// Replaces all occurrences of a string with another string.
mutating func replaceOccurances(of string: String, with: String)

/// Removes all occurrences of the given string.
mutating func removeOccurances(of string: String)

/// URL-encodes the receiver.
mutating func urlEncode()

/// URL-decodes the receiver.
mutating func urlDecode()

Operators

/// Returns the character at the specified index.
subscript(_ index: Int) -> Character

/// Returns a new string containing the characters of the receiver within the range.
subscript(_ range: Range<Int>) -> String

/// Returns a new string containing the characters of the receiver within the range.
subscript (_ range: CountableClosedRange<Int>) -> String

/// Returns a new string containing the characters of the receiver from the one at an index.
subscript (_ range: CountablePartialRangeFrom<Int>) -> String

/// Returns a new string containing the characters of the receiver to the one at an index.
subscript (_ range: PartialRangeUpTo<Int>) -> String

/// Returns a new string containing the characters of the receiver to the one at an index.
subscript (_ range: PartialRangeThrough<Int>) -> String

/// Returns a new string containing the characters of the receiver within the range.
///
/// - Important: This does not return the same result as `NSString.substring(with:)`.
///              It uses actual characters as opposed to `NSString`, which uses UTF-16 characters.
subscript (_ aRange: NSRange) -> String

Protocol Conformance

extension String: Identifiable {
  public var id: String
}
Clone this wiki locally