Structure

Character

A single extended grapheme cluster that approximates a user-perceived character.

Declaration

struct Character

Overview

The Character type represents a character made up of one or more Unicode scalar values, grouped by a Unicode boundary algorithm. Generally, a Character instance matches what the reader of a string will perceive as a single character. Strings are collections of Character instances, so the number of visible characters is generally the most natural way to count the length of a string.

let greeting = "Hello! 🐥"
print("Length: \(greeting.count)")
// Prints "Length: 8"

Because each character in a string can be made up of one or more Unicode scalar values, the number of characters in a string may not match the length of the Unicode scalar value representation or the length of the string in a particular binary representation.

print("Unicode scalar value count: \(greeting.unicodeScalars.count)")
// Prints "Unicode scalar value count: 15"

print("UTF-8 representation count: \(greeting.utf8.count)")
// Prints "UTF-8 representation count: 18"

Every Character instance is composed of one or more Unicode scalar values that are grouped together as an extended grapheme cluster. The way these scalar values are grouped is defined by a canonical, localized, or otherwise tailored Unicode segmentation algorithm.

For example, a country’s Unicode flag character is made up of two regional indicator scalar values that correspond to that country’s ISO 3166-1 alpha-2 code. The alpha-2 code for The United States is “US”, so its flag character is made up of the Unicode scalar values "\u{1F1FA}" (REGIONAL INDICATOR SYMBOL LETTER U) and "\u{1F1F8}" (REGIONAL INDICATOR SYMBOL LETTER S). When placed next to each other in a string literal, these two scalar values are combined into a single grapheme cluster, represented by a Character instance in Swift.

let usFlag: Character = "\u{1F1FA}\u{1F1F8}"
print(usFlag)
// Prints "🇺🇸"

For more information about the Unicode terms used in this discussion, see the Unicode.org glossary. In particular, this discussion mentions extended grapheme clusters and Unicode scalar values.

Topics

Creating a Character

In addition to creating a character from a single-character string literal, you can also convert a unicode scalar value or single-character string.

init(String)

Creates a character from a single-character string.

Writing to a Text Stream

func write<Target>(to: Target)

Writes the character into the given output stream.

Comparing Characters

static func != (Character, Character) -> Bool

Returns a Boolean value indicating whether two values are not equal.

static func <= (Character, Character) -> Bool

Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.

static func > (Character, Character) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.

static func >= (Character, Character) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.

Working with a Character’s Unicode Values

init(Unicode.Scalar)

Creates a character containing the given Unicode scalar value.

var isASCII: Bool

Whether this Character is ASCII.

Beta
var asciiValue: UInt8?

Returns the ASCII encoding value of this Character, if ASCII.

Beta

Inspecting a Character

var isLetter: Bool

Whether this Character is a letter.

Beta
var isPunctuation: Bool

Whether this Character represents punctuation

Beta
var isNewline: Bool

Whether this Character represents a newline.

Beta
var isWhitespace: Bool

Whether this Character represents whitespace, including newlines.

Beta
var isSymbol: Bool

Whether this Character represents a symbol

Beta
var isMathSymbol: Bool

Whether this Character represents a symbol used mathematical formulas

Beta
var isCurrencySymbol: Bool

Whether this Character represents a currency symbol

Beta

Checking a Character’s Case

var isCased: Bool

Whether this Character changes under any form of case conversion.

Beta
var isUppercase: Bool

Whether this Character is considered uppercase.

Beta
func uppercased() -> String

Perform case conversion to uppercase

Beta
var isLowercase: Bool

Whether this Character is considered lowercase.

Beta
func lowercased() -> String

Perform case conversion to lowercase

Beta

Checking a Character’s Numeric Properties

var isNumber: Bool

Whether this Character represents a number.

Beta
var isWholeNumber: Bool

Whether this Character represents a whole number. See Character.wholeNumberValue

Beta
var wholeNumberValue: Int?

If this Character is a whole number, return the value it represents, else nil.

Beta
var isHexDigit: Bool

Whether this Character represents a hexadecimal digit.

Beta
var hexDigitValue: Int?

If this Character is a hexadecimal digit, returns the value it represents, else nil.

Beta

Creating a Range Expression

static func ..< (Character, Character) -> Range<Character>

Returns a half-open range that contains its lower bound but not its upper bound.

static func ... (Character, Character) -> ClosedRange<Character>

Returns a closed range that contains both of its bounds.

static func ..< (Character) -> PartialRangeUpTo<Character>

Returns a partial range up to, but not including, its upper bound.

static func ... (Character) -> PartialRangeThrough<Character>

Returns a partial range up to, and including, its upper bound.

static func ... (Character) -> PartialRangeFrom<Character>

Returns a partial range extending upward from a lower bound.

Describing a Character

var debugDescription: String

A textual representation of the character, suitable for debugging.

var customMirror: Mirror

A mirror that reflects the Character instance.

var customPlaygroundQuickLook: _PlaygroundQuickLook

A custom playground Quick Look for the Character instance.

Deprecated
func hash(into: Hasher)

Hashes the essential components of this value by feeding them into the given hasher.

Infrequently Used Functionality

init(extendedGraphemeClusterLiteral: Character)

Creates a character with the specified value.

init(unicodeScalarLiteral: Character)

Creates an instance initialized to the given value.

See Also

Strings and Characters

struct String

A Unicode string value that is a collection of characters.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software