<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCharacterSet",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCharacterSet"
  },
  "title" : "NSCharacterSet"
}
-->

# NSCharacterSet

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

```
class NSCharacterSet
```

## Overview

In Swift, this bridges to a [`CharacterSet`](/documentation/Foundation/CharacterSet); use [`NSCharacterSet`](/documentation/Foundation/NSCharacterSet) when you need reference semantics or other Foundation-specific behavior.

An `NSCharacterSet` object represents a set of Unicode-compliant characters. `NSString` and `NSScanner` objects use `NSCharacterSet` objects to group characters together for searching operations, so that they can find any of a particular set of characters during a search. The cluster’s two public classes, `NSCharacterSet` and [`NSMutableCharacterSet`](/documentation/Foundation/NSMutableCharacterSet), declare the programmatic interface for static and dynamic character sets, respectively.

The objects you create using these classes are referred to as character set objects (and when no confusion will result, merely as character sets). Because of the nature of class clusters, character set objects aren’t actual instances of the `NSCharacterSet` or `NSMutableCharacterSet` classes but of one of their private subclasses. Although a character set object’s class is private, its interface is public, as declared by these abstract superclasses, `NSCharacterSet` and `NSMutableCharacterSet`. The character set classes adopt the `NSCopying` and `NSMutableCopying` protocols, making it convenient to convert a character set of one type to the other.

The `NSCharacterSet` class declares the programmatic interface for an object that manages a set of Unicode characters (see the [`NSString`](/documentation/Foundation/NSString) class cluster specification for information on Unicode). `NSCharacterSet`’s principal primitive method, [`characterIsMember(_:)`](/documentation/Foundation/NSCharacterSet/characterIsMember(_:)), provides the basis for all other instance methods in its interface. A subclass of `NSCharacterSet` needs only to implement this method, plus [`mutableCopy(with:)`](/documentation/Foundation/NSMutableCopying/mutableCopy(with:)), for proper behavior. For optimal performance, a subclass should also override [`bitmapRepresentation`](/documentation/Foundation/NSCharacterSet/bitmapRepresentation), which otherwise works by invoking [`characterIsMember(_:)`](/documentation/Foundation/NSCharacterSet/characterIsMember(_:)) for every possible Unicode value.

`NSCharacterSet` is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFCharacterSet>. See [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information on toll-free bridging.

> Important:
> The Swift overlay to the Foundation framework provides the ``doc://com.apple.foundation/documentation/Foundation/CharacterSet`` structure, which bridges to the ``doc://com.apple.foundation/documentation/Foundation/NSCharacterSet`` class and its mutable subclass, ``doc://com.apple.foundation/documentation/Foundation/NSMutableCharacterSet``. For more information about value types, see <doc://com.apple.documentation/documentation/Swift/working-with-foundation-types>.

## Topics

### Getting Standard Character Sets

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

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

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

A character set containing the characters in Unicode General Category Lt.

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

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

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

A character set containing the characters in the category of Decimal Numbers.

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

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/NSCharacterSet/illegalCharacters)

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/NSCharacterSet/letters)

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

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

A character set containing the characters in Unicode General Category Ll.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

### Getting Character Sets for URL Encoding

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

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

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

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

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

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

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

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

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

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

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

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

### Creating a Custom Character Set

[`init(coder:)`](/documentation/Foundation/NSCharacterSet/init(coder:))

[`init(charactersIn:)`](/documentation/Foundation/NSCharacterSet/init(charactersIn:))

Returns a character set containing the characters in a given string.

[`init(range:)`](/documentation/Foundation/NSCharacterSet/init(range:))

Returns a character set containing characters with Unicode values in a given range.

[NSOpenStepUnicodeReservedBase](/documentation/Foundation/1560803-nsopenstepunicodereservedbase)

Specifies lower bound for a Unicode character range reserved for Apple’s corporate use.

### Creating and Managing Character Sets as Bitmap Representations

[`init(bitmapRepresentation:)`](/documentation/Foundation/NSCharacterSet/init(bitmapRepresentation:))

Returns a character set containing characters determined by a given bitmap representation.

[`init(contentsOfFile:)`](/documentation/Foundation/NSCharacterSet/init(contentsOfFile:))

Returns a character set read from the bitmap representation stored in the file a given path.

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

An `NSData` object encoding the receiver in binary format.

### Inverting a Character Set

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

A character set containing only characters that don’t exist in the receiver.

### Testing Set Membership

[`characterIsMember(_:)`](/documentation/Foundation/NSCharacterSet/characterIsMember(_:))

Returns a Boolean value that indicates whether a given character is in the receiver.

[`hasMemberInPlane(_:)`](/documentation/Foundation/NSCharacterSet/hasMemberInPlane(_:))

Returns a Boolean value that indicates whether the receiver has at least one member in a given character plane.

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

Returns a Boolean value that indicates whether the receiver is a superset of another given character set.

[`longCharacterIsMember(_:)`](/documentation/Foundation/NSCharacterSet/longCharacterIsMember(_:))

Returns a Boolean value that indicates whether a given long character is a member of the receiver.



---

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)