ADMaskedStringFormatter

public class ADMaskedStringFormatter

A ADMaskedStringFormatter takes a formatting string and applies it as a mask to a given string value. For example, applying format string (###) ###-#### to 8085551212 would result in (808) 555-1212. A ADMaskedStringFormatter can also be used to remove a format from a string, using the example above, given (808) 555-1212 would result in 8085551212.

Supported Format Characters

  • # - An optional number (0-9). If a number is available at the current character location, the number is emitted, else a space () will be emitted.
  • 0 - A fixed number (0-9). If a number is available at the current character location, the number is emitted, else a zero (0) will be emitted.
  • X - An uppercased letter (A-Z). If a letter is available, it will be converted to upper case and emitted, else a space () is emitted.
  • x - A lowercased letter (a-z). If a letter is available, it will be converted to lower case and emitted, else a space () is emitted.
  • ***** - Any letter (A-Z or a-z). If a letter is available, it will be emitted, else a space () is emitted.
  • _ - Any character. No matter what the character is, it will be emitted as-is.
  • *\* - Escapes the next character and includes it in the formatted output. Use \\ to include a \\, #, 0, X, x, * or _ character in the output.

Any other character in the format string will be output as-is in the formatted string.

Example

// Configure formatter
let formatter = ADMaskedStringFormatter()
formatter.formatString = "(###) ###-####"

// Convert output
let output = formatter.applyFormat(to: "8085551212")
  • Defines the mask that will be applied to a given string to create the formatted output.

    Supported Format Characters

    • # - An optional number (0-9). If a number is available at the current character location, the number is emitted, else a space () will be emitted.
    • 0 - A fixed number (0-9). If a number is available at the current character location, the number is emitted, else a zero (0) will be emitted.
    • X - An uppercased letter (A-Z). If a letter is available, it will be converted to upper case and emitted, else a space () is emitted.
    • x - A lowercased letter (a-z). If a letter is available, it will be converted to lower case and emitted, else a space () is emitted.
    • ***** - Any letter (A-Z or a-z). If a letter is available, it will be emitted, else a space () is emitted.
    • _ - Any character. No matter what the character is, it will be emitted as-is.
    • *\* - Escapes the next character and includes it in the formatted output. Use \\ to include a \\, #, 0, X, x, * or _ character in the output.

    Any other character in the format string will be output as-is in the formatted string.

    Example

    // Configure formatter
    let formatter = ADMaskedStringFormatter()
    formatter.formatString = "(###) ###-####"
    
    // Convert output
    let output = formatter.applyFormat(to: "8085551212")
    

    Declaration

    Swift

    public var formatString: String = ""
  • Returns true if the input string given to the applyFormat function can be properly formatted against the pattern in the formatString property.

    Example

    // Configure formatter
    let formatter = ADMaskedStringFormatter()
    formatter.formatString = "(###) ###-####"
    
    // Convert output
    let output = formatter.applyFormat(to: "8085551212")
    let valid = formatter.validForLastInput
    

    Declaration

    Swift

    public var validForLastInput: Bool = true
  • Initializes a new instance of the ADMaskedStringFormatter.

    Declaration

    Swift

    public init()
  • Applies the formatString to the given string to return a formatted string.

    Example

    // Configure formatter
    let formatter = ADMaskedStringFormatter()
    formatter.formatString = "(###) ###-####"
    
    // Convert output
    let output = formatter.applyFormat(to: "8085551212")
    let valid = formatter.validForLastInput
    

    Remark

    Sets the validForLastInput to true if the format was successfully applied, else it is set to false.

    Declaration

    Swift

    public func applyFormat(to text: String) -> String

    Parameters

    text

    The string to format.

    Return Value

    The given string with the format applied to it.

  • Strips the format specified in the formatString property from the given string and returns the unformatted value.

    Example

    // Configure formatter
    let formatter = ADMaskedStringFormatter()
    formatter.formatString = "(###) ###-####"
    
    // Convert output
    let output = formatter.applyFormat(to: "8085551212")
    let valid = formatter.validForLastInput
    
    // Strip format
    let result = formatter.removeFormat(from: output)
    

    Declaration

    Swift

    public func removeFormat(from text: String) -> String

    Parameters

    text

    The value to remove the formatting from.

    Return Value

    The input string with the formatting removed.