<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URL/FormatStyle",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation3URLV11FormatStyleV"
  },
  "title" : "URL.FormatStyle"
}
-->

# URL.FormatStyle

A structure that converts between URL instances and their textual representations.

```
struct FormatStyle
```

## Overview

Instances of [`URL.FormatStyle`](/documentation/Foundation/URL/FormatStyle) create localized, human-readable text from [`URL`](/documentation/Foundation/URL) instances and parse string representations of URLs into instances of [`URL`](/documentation/Foundation/URL).

### Formatting URLs

Use the [`formatted()`](/documentation/Foundation/URL/formatted()) method to create a string representation of a URL using the default [`URL.FormatStyle`](/documentation/Foundation/URL/FormatStyle) configuration. As seen in the following example, the default style creates a string with the scheme, host, and path, but not the port or query.

```swift
let url = URL(string:"https://www.example.com:8080/path/to/endpoint?key=value")!
let formatted = url.formatted() // "https://www.example.com/path/to/endpoint"
```

You can specify a format style by providing an argument to the [`format(_:)`](/documentation/Foundation/URL/FormatStyle/format(_:)) method. The following example uses the previous URL, but preserves only the host and path.

```swift
let url = URL(string:"https://www.example.com:8080/path/to/endpoint?key=value")!
let style = URL.FormatStyle(scheme: .never,
                            user: .never,
                            password: .never,
                            host: .always,
                            port: .never,
                            path: .always,
                            query: .never,
                            fragment: .never)
let formatted = style.format(url) // "www.example.com/path/to/endpoint"
```

Instantiate a style when you want to format multiple URL instances with the same style. For one-time access to a default style, you can use the static accessor [`url`](/documentation/Foundation/FormatStyle/url) at call points that expect the [`URL.FormatStyle`](/documentation/Foundation/URL/FormatStyle) type, such as the [`format(_:)`](/documentation/Foundation/URL/FormatStyle/format(_:)) method. This means you can write the example above as follows:

```swift
let url = URL(string:"https://www.example.com:8080/path/to/endpoint?key=value")!
let formatted = url.formatted(.url
    .scheme(.never)
    .host(.always)
    .port(.never)
    .path(.always)
    .query(.never)) // "www.example.com/path/to/endpoint"
```

This example works by taking the default style provided by [`url`](/documentation/Foundation/FormatStyle/url), then customizing it with calls to the style modifiers in Customizing style behavior.

### Parsing URLs

You can use [`URL.FormatStyle`](/documentation/Foundation/URL/FormatStyle) to parse strings into URL values. To do this, create a [`URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy) from a format style, then call the strategy’s [`parse(_:)`](/documentation/Foundation/URL/ParseStrategy/parse(_:)) method.

```swift
let style = URL.FormatStyle(scheme: .always,
                            user: .never,
                            password: .never,
                            host: .always,
                            port: .always,
                            path: .always,
                            query: .always,
                            fragment: .never)
let urlString = "https://www.example.com:8080/path/to/endpoint?key=value"
let url = try? style.parseStrategy.parse(urlString)
```

### Matching regular expressions

Along with parsing URL values in strings, you can use the regular expression domain-specific language provided by Swift to match and capture URL substrings. The following example scans source input that’s expected to contain a timestamp, some whitespace, and a URL.

```swift
import RegexBuilder
let source = "7/31/2022, 5:15:12 AM  https://www.example.com/productList?query=slushie"
let matcher = Regex {
    One(.dateTime(date: .numeric,
                  time: .standard,
                  locale: Locale(identifier: "en_US"),
                  timeZone: TimeZone(identifier: "PST")!))
    OneOrMore(.horizontalWhitespace)
    Capture {
        One(.url(scheme: .required,
                 user: .optional,
                 password: .optional,
                 host: .required,
                 port: .defaultValue(8088),
                 path: .optional,
                 query: .optional,
                 fragment: .optional))
    }
}
guard let match = source.firstMatch(of: matcher) else { return }
let url = match.1 // url = https://www.example.com:8088/productList?query=slushie
```

## Topics

### Creating a URL format style

[`init(scheme:user:password:host:port:path:query:fragment:)`](/documentation/Foundation/URL/FormatStyle/init(scheme:user:password:host:port:path:query:fragment:))

Creates a URL format style with the given display options.

[`URL.FormatStyle.ComponentDisplayOption`](/documentation/Foundation/URL/FormatStyle/ComponentDisplayOption)

A type that indicates whether a formatted URL should include a component.

[`URL.FormatStyle.HostDisplayOption`](/documentation/Foundation/URL/FormatStyle/HostDisplayOption)

A type that indicates whether a formatted URL should include the host component.

### Formatting URL values

[`format(_:)`](/documentation/Foundation/URL/FormatStyle/format(_:))

Formats a URL, using this style.

### Customizing style behavior

[`scheme(_:)`](/documentation/Foundation/URL/FormatStyle/scheme(_:))

Modifies a format style to display a URL’s scheme component in accordance with the provided option.

[`user(_:)`](/documentation/Foundation/URL/FormatStyle/user(_:))

Modifies a format style to display a URL’s user component in accordance with the provided option.

[`password(_:)`](/documentation/Foundation/URL/FormatStyle/password(_:))

Modifies a format style to display a URL’s password component in accordance with the provided option.

[`host(_:)`](/documentation/Foundation/URL/FormatStyle/host(_:))

Modifies a format style to display a URL’s host component in accordance with the provided option.

[`URL.FormatStyle.HostDisplayOption`](/documentation/Foundation/URL/FormatStyle/HostDisplayOption)

A type that indicates whether a formatted URL should include the host component.

[`port(_:)`](/documentation/Foundation/URL/FormatStyle/port(_:))

Modifies a format style to display a URL’s port component in accordance with the provided option.

[`path(_:)`](/documentation/Foundation/URL/FormatStyle/path(_:))

Modifies a format style to display a URL’s path component in accordance with the provided option.

[`query(_:)`](/documentation/Foundation/URL/FormatStyle/query(_:))

Modifies a format style to display a URL’s query component in accordance with the provided option.

[`fragment(_:)`](/documentation/Foundation/URL/FormatStyle/fragment(_:))

Modifies a format style to display a URL’s fragment component in accordance with the provided option.

[`URL.FormatStyle.ComponentDisplayOption`](/documentation/Foundation/URL/FormatStyle/ComponentDisplayOption)

A type that indicates whether a formatted URL should include a component.

### Applying currency styles

### Applying measurement styles

### Applying list styles

### Parsing URLs

[`parseStrategy`](/documentation/Foundation/URL/FormatStyle/parseStrategy)

The parse strategy used by this format style.

[`URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy)

A parse strategy for creating URLs from formatted strings.

### Supporting types



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)