String

extension String
  • Returns a localized string pulled from Localizable.strings by its key.

    Declaration

    Swift

    @inlinable
    public var localized: String { get }
  • Returns a string by pulling a format string from the Localizable.strings file by the key (the string you call this method on), and replacing placeholders with the arguments.

    • Pass any number of arguments, separated by comma (this is called variadic parameter).
    • The number and types of arguments in call must match the number and types of placeholders in the format string. Otherwise a runtime error will occur.
    • It’s a good idea to have at least a rough understanding of how the localization process works. See Introduction to String Programming Guide and, specifically, Formatting String Objects.
    • For a table of available placeholder keys and corresponding data types, see String Format Specifiers.

    Declaration

    Swift

    @inlinable
    public func localized(with arguments: CVarArg...) -> String

    Parameters

    arguments

    Required. Arguments to interpolate in the format string.

  • Returns a string by pulling a format string from the Localizable.strings file by the key (the string you call this method on), and replacing placeholders with the arguments.

    Declaration

    Swift

    public func localized(
        arguments: [CVarArg],
        locale: Locale? = nil
    ) -> String

    Parameters

    arguments

    Required. An array of arguments to interpolate in the format string.

    locale

    Optional. The locale in which to render the localized string. Default is nil, which corresponds to the system locale.

Transformation

  • Returns a new string by applying a sequence of transforms to the string.

    • Transforms are applied in the order they’re specified in.
    • For the list of available transforms, see StringTransform.

    Declaration

    Swift

    @available(iOS 9, *)
    public func applyingTransformChain(_ transformChain: [StringTransform]) -> String?

Comparison

  • Creates a string suitable for comparison by removing insignificant distinctions from the string.

    • To specify more options, call folding(options:locale:) on the string instead.
    • For the full list of options, see NSString.CompareOptions.

    Declaration

    Swift

    public var suitableForComparison: String { get }

    Return Value

    A string created by calling folding(options:locale:) with options caseInsensitive, diacriticInsensitive, and numeric on the string.

Matching Against Regular Expressions

  • Indicates whether the string fully matches (i.e. has exactly one match with) the specified regular expression.

    Declaration

    Swift

    public func matches(
        _ regularExpression: NSRegularExpression,
        withOptions matchingOptions: NSRegularExpression.MatchingOptions = []
    ) -> Bool

    Parameters

    regularExpression

    Required. Regular expression to match the string against.

    matchingOptions

    Optional. Matching options to use. See NSRegularExpression.MatchingOptions. Default is [].

    Return Value

    Bool. Matching result.

  • Indicates whether the string fully matches (i.e. has exactly one match with) a regular expression initialized with the specified pattern with options.

    Declaration

    Swift

    public func matchesRegularExpressionThrowing(
        fromPattern regularExpressionPattern: String,
        withOptions options: NSRegularExpression.Options = []
    ) throws -> Bool

    Parameters

    regularExpressionPattern

    Required. Regular expression pattern to initialize an NSRegularExpression from.

    options

    Optional. NSRegularExpression.Options to use when initializing an NSRegularExpression; default is [].

  • Indicates whether the string fully matches (i.e. has exactly one match with) a regular expression initialized with the specified pattern with options.

    Declaration

    Swift

    public func matchesRegularExpression(
        fromPattern regularExpressionPattern: String,
        withOptions options: NSRegularExpression.Options = []
    ) -> Bool?

    Parameters

    regularExpressionPattern

    Required. Regular expression pattern to initialize an NSRegularExpression from.

    options

    Optional. NSRegularExpression.Options to use when initializing an NSRegularExpression; default is [].

    Return Value

    Bool?. Matching result, if regular expression initialized successfully; otherwise, nil.