<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/CharacterSet",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation12CharacterSetV"
  },
  "title" : "CharacterSet"
}
-->

# CharacterSet

A set of Unicode character values for use in search operations.

```
struct CharacterSet
```

## Overview

A `CharacterSet` represents a set of Unicode-compliant characters. Foundation types use `CharacterSet` to group characters together for searching operations, so that they can find any of a particular set of characters during a search.

This type provides “copy-on-write” behavior, and is also bridged to the Objective-C `NSCharacterSet` class.

## Topics

### Getting Standard Character Sets

[`alphanumerics`](/documentation/Foundation/CharacterSet/alphanumerics)

Returns a character set containing the characters in Unicode General Categories L*, M*, and N*.

[`capitalizedLetters`](/documentation/Foundation/CharacterSet/capitalizedLetters)

Returns a character set containing the characters in Unicode General Category Lt.

[`controlCharacters`](/documentation/Foundation/CharacterSet/controlCharacters)

Returns a character set containing the characters in Unicode General Category Cc and Cf.

[`decimalDigits`](/documentation/Foundation/CharacterSet/decimalDigits)

Returns a character set containing the characters in the category of Decimal Numbers.

[`decomposables`](/documentation/Foundation/CharacterSet/decomposables)

Returns a character set containing individual Unicode characters that can also be represented as composed character sequences (such as for letters with accents), by the definition of “standard decomposition” in version 3.2 of the Unicode character encoding standard.

[`illegalCharacters`](/documentation/Foundation/CharacterSet/illegalCharacters)

Returns a character set containing values in the category of Non-Characters or that have not yet been defined in version 3.2 of the Unicode standard.

[`letters`](/documentation/Foundation/CharacterSet/letters)

Returns a character set containing the characters in Unicode General Category L* & M*.

[`lowercaseLetters`](/documentation/Foundation/CharacterSet/lowercaseLetters)

Returns a character set containing the characters in Unicode General Category Ll.

[`newlines`](/documentation/Foundation/CharacterSet/newlines)

Returns a character set containing the newline characters (`U+000A ~ U+000D`, `U+0085`, `U+2028`, and `U+2029`).

[`nonBaseCharacters`](/documentation/Foundation/CharacterSet/nonBaseCharacters)

Returns a character set containing the characters in Unicode General Category M*.

[`punctuationCharacters`](/documentation/Foundation/CharacterSet/punctuationCharacters)

Returns a character set containing the characters in Unicode General Category P*.

[`symbols`](/documentation/Foundation/CharacterSet/symbols)

Returns a character set containing the characters in Unicode General Category S*.

[`uppercaseLetters`](/documentation/Foundation/CharacterSet/uppercaseLetters)

Returns a character set containing the characters in Unicode General Category Lu and Lt.

[`whitespaces`](/documentation/Foundation/CharacterSet/whitespaces)

Returns a character set containing the characters in Unicode General Category Zs and `CHARACTER TABULATION (U+0009)`.

[`whitespacesAndNewlines`](/documentation/Foundation/CharacterSet/whitespacesAndNewlines)

Returns a character set containing characters in Unicode General Category Z*, `U+000A ~ U+000D`, and `U+0085`.

### Getting Character Sets for URL Encoding

[`urlFragmentAllowed`](/documentation/Foundation/CharacterSet/urlFragmentAllowed)

Returns the character set for characters allowed in a fragment URL component.

[`urlHostAllowed`](/documentation/Foundation/CharacterSet/urlHostAllowed)

Returns the character set for characters allowed in a host URL subcomponent.

[`urlPasswordAllowed`](/documentation/Foundation/CharacterSet/urlPasswordAllowed)

Returns the character set for characters allowed in a password URL subcomponent.

[`urlPathAllowed`](/documentation/Foundation/CharacterSet/urlPathAllowed)

Returns the character set for characters allowed in a path URL component.

[`urlQueryAllowed`](/documentation/Foundation/CharacterSet/urlQueryAllowed)

Returns the character set for characters allowed in a query URL component.

[`urlUserAllowed`](/documentation/Foundation/CharacterSet/urlUserAllowed)

Returns the character set for characters allowed in a user URL subcomponent.

### Creating a Custom Character Set

[`init()`](/documentation/Foundation/CharacterSet/init())

Initialize an empty instance.

### Creating and Managing Bitmap Representations

[`bitmapRepresentation`](/documentation/Foundation/CharacterSet/bitmapRepresentation)

Returns a representation of the `CharacterSet` in binary format.

### Inverting a Character Set

[`invert()`](/documentation/Foundation/CharacterSet/invert())

Invert the contents of the `CharacterSet`.

[`inverted`](/documentation/Foundation/CharacterSet/inverted)

Returns an inverted copy of the receiver.

### Combining Character Sets

[`formIntersection(_:)`](/documentation/Foundation/CharacterSet/formIntersection(_:))

Sets the value to an intersection of the `CharacterSet` with another `CharacterSet`.

[`formSymmetricDifference(_:)`](/documentation/Foundation/CharacterSet/formSymmetricDifference(_:))

Sets the value to an exclusive or of the `CharacterSet` with another `CharacterSet`.

[`formUnion(_:)`](/documentation/Foundation/CharacterSet/formUnion(_:))

Sets the value to a union of the `CharacterSet` with another `CharacterSet`.

[`hasMember(inPlane:)`](/documentation/Foundation/CharacterSet/hasMember(inPlane:))

Returns true if the `CharacterSet` has a member in the specified plane.

[`insert(charactersIn:)`](/documentation/Foundation/CharacterSet/insert(charactersIn:)-2syuj)

Insert the values from the specified string into the `CharacterSet`.

[`intersection(_:)`](/documentation/Foundation/CharacterSet/intersection(_:))

Returns an intersection of the `CharacterSet` with another `CharacterSet`.

[`invert()`](/documentation/Foundation/CharacterSet/invert())

Invert the contents of the `CharacterSet`.

[`isSuperset(of:)`](/documentation/Foundation/CharacterSet/isSuperset(of:))

Returns true if `self` is a superset of `other`.

[`remove(charactersIn:)`](/documentation/Foundation/CharacterSet/remove(charactersIn:)-3sayw)

Remove the values from the specified string from the `CharacterSet`.

[`subtracting(_:)`](/documentation/Foundation/CharacterSet/subtracting(_:))

Returns a `CharacterSet` created by removing elements in `other` from `self`.

[`symmetricDifference(_:)`](/documentation/Foundation/CharacterSet/symmetricDifference(_:))

Returns an exclusive or of the `CharacterSet` with another `CharacterSet`.

[`union(_:)`](/documentation/Foundation/CharacterSet/union(_:))

Returns a union of the `CharacterSet` with another `CharacterSet`.

### Adding Characters

[`insert(charactersIn:)`](/documentation/Foundation/CharacterSet/insert(charactersIn:)-2syuj)

Insert the values from the specified string into the `CharacterSet`.

### Removing Characters

[`remove(charactersIn:)`](/documentation/Foundation/CharacterSet/remove(charactersIn:)-3sayw)

Remove the values from the specified string from the `CharacterSet`.

[`subtracting(_:)`](/documentation/Foundation/CharacterSet/subtracting(_:))

Returns a `CharacterSet` created by removing elements in `other` from `self`.

### Testing Set Membership

[`hasMember(inPlane:)`](/documentation/Foundation/CharacterSet/hasMember(inPlane:))

Returns true if the `CharacterSet` has a member in the specified plane.

[`isSuperset(of:)`](/documentation/Foundation/CharacterSet/isSuperset(of:))

Returns true if `self` is a superset of `other`.

### Comparing Character Sets

[`==(_:_:)`](/documentation/Foundation/CharacterSet/==(_:_:))

Returns true if the two `CharacterSet`s are equal.

### Using Reference Types

[`NSCharacterSet`](/documentation/Foundation/NSCharacterSet)

An object representing a fixed set of Unicode character values for use in search operations.

[`NSMutableCharacterSet`](/documentation/Foundation/NSMutableCharacterSet)

An object representing a mutable set of Unicode character values for use in search operations.

## Relationships

### Conforms To

[`SetAlgebra`](/documentation/Swift/SetAlgebra)

[`Decodable`](/documentation/Swift/Decodable)

[`Copyable`](/documentation/Swift/Copyable)

[`Sendable`](/documentation/Swift/Sendable)

[`Hashable`](/documentation/Swift/Hashable)

[`ReferenceConvertible`](/documentation/Foundation/ReferenceConvertible)

[`ExpressibleByArrayLiteral`](/documentation/Swift/ExpressibleByArrayLiteral)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Equatable`](/documentation/Swift/Equatable)

[`Escapable`](/documentation/Swift/Escapable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Encodable`](/documentation/Swift/Encodable)

---

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)