String

public extension String

This set of extensions provides a variety of functionality for string manipulation and validation in Swift. It includes properties for obtaining modified versions of the string, properties for converting the string to coordinate or URL representations, and functions for appending or deleting path components and extensions. Additionally, there are functions for string manipulation, such as checking for digit or alphanumeric content, adding prefixes, and validating email addresses and phone numbers.

  • A string with leading and trailing whitespaces removed.

    Declaration

    Swift

    var trimmed: String { get }
  • Modifies the string in place by removing leading and trailing whitespaces.

    Example:

      var text = "   Hello, World!   "
      text.trim()
      print(text) // Output: "Hello, World!"
    

    Declaration

    Swift

    mutating func trim()
  • An optional CLLocationCoordinate2D representation of the string assuming it represents coordinates in the format “latitude, longitude”.

    Declaration

    Swift

    var asCoordinates: CLLocationCoordinate2D? { get }
  • An optional URL representation of the string.

    Declaration

    Swift

    var asURL: URL? { get }
  • The last component of the path.

    Declaration

    Swift

    var lastPathComponent: String { get }
  • The path extension of the string.

    Declaration

    Swift

    var pathExtension: String { get }
  • A string formed by deleting the last path component from the original string.

    Declaration

    Swift

    var stringByDeletingLastPathComponent: String { get }
  • A string formed by deleting the path extension from the original string.

    Declaration

    Swift

    var stringByDeletingPathExtension: String { get }
  • An array of path components in the string.

    Declaration

    Swift

    var pathComponents: [String] { get }
  • Appends a path component to the string.

    Example:

      let basePath = "/Users/username/Documents"
      let newPath = basePath.stringByAppendingPathComponent(path: "file.txt")
      print(newPath) // Output: "/Users/username/Documents/file.txt"
    

    Declaration

    Swift

    func stringByAppendingPathComponent(path: String) -> String

    Parameters

    path

    The path component to append.

    Return Value

    A new string formed by appending the given path component.

  • Appends a path extension to the string.

    Example:

      let fileName = "document"
      let newFileName = fileName.stringByAppendingPathExtension(ext: "txt")
      print(newFileName) // Output: "document.txt"
    

    Declaration

    Swift

    func stringByAppendingPathExtension(ext: String) -> String?

    Parameters

    ext

    The path extension to append.

    Return Value

    A new string formed by appending the given path extension.

  • Checks if the string contains only digits.

    Declaration

    Swift

    var containsOnlyDigits: Bool { get }
  • Checks if the string is alphanumeric.

    Declaration

    Swift

    var isAlphaNumeric: Bool { get }
  • Prepends a prefix to the string.

    Example:

      let word = "Swift"
      let newWord = word.withPrefix("Hello ")
      print(newWord) // Output: "Hello Swift"
    

    Declaration

    Swift

    func withPrefix(_ prefix: String) -> String

    Parameters

    prefix

    The prefix to add.

    Return Value

    A new string with the specified prefix.

  • Checks if the string represents a valid email address.

    Declaration

    Swift

    func isEmailValid() -> Bool

    Return Value

    A boolean indicating whether the string is a valid email address.

  • Checks if the string represents a valid phone number.

    Declaration

    Swift

    func isPhoneNumberValid() -> Bool

    Return Value

    A boolean indicating whether the string is a valid phone number.

  • Modifies the string by capitalizing its first letter.

    Example:

      var text = "hello world"
      text.capitalizeFirstLetter()
      print(text) // Output: "Hello world"
    

    Declaration

    Swift

    mutating func capitalizeFirstLetter()
  • To convert the words “true” or “false” to the boolean value

    Example:

     let testTrueBool = "true".boolValue // true
     let testFalseBool = "false".boolValue  // false
    

    Declaration

    Swift

    var toBool: Bool { get }
  • Converts the string representation of a date to a Date object using the specified format.

    Example:

      let dateString = "2023-01-15"
      if let date = dateString.toDate(format: "yyyy-MM-dd") {
          print(date) // Output: 2023-01-15 00:00:00 +0000
      }
    

    Declaration

    Swift

    func toDate(format: String) -> Date?

    Parameters

    format

    The format of the date string.

    Return Value

    A Date object representing the parsed date, or nil if parsing fails.