String - Swift Standard Library - Apple Developer Documentation
String - Swift Standard Library - Apple Developer Documentation
Structure
String
A Unicode string value that is a collection of characters. SDK
Xcode 6.0.1+
Framework
You can create new strings using string literals or string interpolations. A string literal is a series
of characters enclosed in quotes.
String interpolations are string literals that evaluate any included expressions and convert the
results to string form. String interpolations give you an easy way to build a string from multiple
pieces. Wrap each expression in a string interpolation in parentheses, prefixed by a backslash.
let price = 2
let number = 3
let cookiePrice = "\(number) cookies: $\(price * number)."
// cookiePrice == "3 cookies: $6."
Multiline string literals are enclosed in three double quotation marks ("""), with each delimiter
on its own line. Indentation is stripped from each line of a multiline string literal to match the
indentation of the closing delimiter.
print(greeting)
// Prints "Welcome!"
Comparing strings for equality using the equal-to operator (==) or a relational operator (like < or
>=) is always performed using Unicode canonical representation. As a result, different
representations of a string compare as being equal.
The Unicode scalar value "\u{301}" modifies the preceding character to include an accent,
so "e\u{301}" has the same canonical representation as the single Unicode scalar value "é".
Basic string operations are not sensitive to locale settings, ensuring that string comparisons
and other operations always have a single, stable result, allowing strings to be used as keys in
Dictionary instances and for other purposes.
For example, to retrieve the first word of a longer string, you can search for a space and then
create a substring from a prefix of the string up to that point:
The firstName constant is an instance of the Substring type—a type that represents
substrings of a string while sharing the original stringʼs storage. Substrings present the same
interface as strings.
To demonstrate the different views available for every string, the following examples use this
String instance:
print(cafe.count)
// Prints "9"
print(Array(cafe))
// Prints "["C", "a", "f", "é", " ", "d", "u", " ", " 🌍 "]"
print(cafe.unicodeScalars.count)
// Prints "10"
print(Array(cafe.unicodeScalars))
// Prints "["C", "a", "f", "e", "\u{0301}", " ", "d", "u", " ", "\u{0001F30D}"]"
print(cafe.unicodeScalars.map { $0.value })
// Prints "[67, 97, 102, 101, 769, 32, 100, 117, 32, 127757]"
The unicodeScalars viewʼs elements comprise each Unicode scalar value in the cafe string.
In particular, because cafe was declared using the decomposed form of the "é" character,
unicodeScalars contains the scalar values for both the letter "e" (101) and the accent
character "´" (769).
UTF-16 View
A stringʼs utf16 property is a collection of UTF-16 code units, the 16-bit encoding form of the
stringʼs Unicode scalar values. Each code unit is stored as a UInt16 instance.
print(cafe.utf16.count)
// Prints "11"
print(Array(cafe.utf16))
// Prints "[67, 97, 102, 101, 769, 32, 100, 117, 32, 55356, 57101]"
The elements of the utf16 view are the code units for the string when encoded in UTF-16.
These elements match those accessed through indexed NSString APIs.
UTF-8 View
A stringʼs utf8 property is a collection of UTF-8 code units, the 8-bit encoding form of the
stringʼs Unicode scalar values. Each code unit is stored as a UInt8 instance.
print(cafe.utf8.count)
// Prints "14"
print(Array(cafe.utf8))
// Prints "[67, 97, 102, 101, 204, 129, 32, 100, 117, 32, 240, 159, 140, 141]"
The elements of the utf8 view are the code units for the string when encoded in UTF-8. This
representation matches the one used when String instances are passed to C APIs.
For example, an ASCII character like the capital letter A is represented by a single element in
each of its four views. The Unicode scalar value of A is 65, which is small enough to fit in a
single code unit in both UTF-16 and UTF-8.
On the other hand, an emoji flag character is constructed from a pair of Unicode scalar values,
like "\u{1F1F5}" and "\u{1F1F7}". Each of these scalar values, in turn, is too large to fit
into a single UTF-16 or UTF-8 code unit. As a result, each view of the string " " reports a
different length.
Strings and their views share indices, so you can access the UTF-8 view of the name string
using the same firstSpace index.
print(Array(name.utf8[..<firstSpace]))
// Prints "[77, 97, 114, 105, 101]"
Note that an index into one view may not have an exact corresponding position in another view.
For example, the flag string declared above comprises a single character, but is composed of
eight code units when encoded as UTF-8. The following code creates constants for the first and
second positions in the flag.utf8 view. Accessing the utf8 view with these indices yields
the first and second code UTF-8 units.
When used to access the elements of the flag string itself, however, the secondCodeUnit
index does not correspond to the position of a specific character. Instead of only accessing the
specific UTF-8 code unit, that index is treated as the position of the character at the indexʼs
encoded offset. In the case of secondCodeUnit, that character is still the flag itself.
If you need to validate that an index from one stringʼs view corresponds with an exact position
in another view, use the indexʼs samePosition(in:) method or the init(_:within:)
initializer.
Performance Optimizations
Although strings in Swift have value semantics, strings use a copy-on-write strategy to store
their data in a buffer. This buffer can then be shared by different copies of a string. A stringʼs
data is only copied lazily, upon mutation, when more than one string instance is using the same
buffer. Therefore, the first in any sequence of mutating operations may cost O(n) time and
space.
When a stringʼs contiguous storage fills up, a new buffer must be allocated and data must be
moved to the new storage. String buffers use an exponential growth strategy that makes
appending to a string a constant time operation when averaged over many append operations.
For more information about the Unicode terms used in this discussion, see the Unicode.org
glossary. In particular, this discussion mentions extended grapheme clusters, Unicode scalar
values, and canonical equivalence.
Topics
Creating a String In addition to creating a string from a single string literal, you can also create an empty string,
a string containing an existing group of characters, or a string repeating the contents of
another string.
init()
Creates an empty string.
init(Character)
Creates a string containing the given character.
init<S>(S)
Creates a new string containing the characters in the given sequence.
init<S>(S)
Creates a new instance of a collection containing the elements of a sequence.
init<S>(S)
Creates a new string containing the characters in the given sequence.
init(Substring)
Creates a new string from the given substring.
init(repeating: String, count: Int)
Creates a new string representing the given string repeated the specified number of
times.
init?(utf8String: UnsafePointer<CChar>)
Produces a string created by copying the data from a given C array of UTF8-encoded
bytes.
init?(validatingUTF8: UnsafePointer<CChar>)
Creates a new string by copying and validating the null-terminated UTF-8 data
referenced by the given pointer.
init(cString: UnsafePointer<CChar>)
Creates a new string by copying the null-terminated UTF-8 data referenced by the given
pointer.
init(cString: UnsafePointer<UInt8>)
Creates a new string by copying the null-terminated UTF-8 data referenced by the given
pointer.
init<Encoding>(decodingCString: UnsafePointer<Encoding.CodeUnit>,
as: Encoding.Type)
Creates a string from the null-terminated sequence of bytes at the given pointer.
init<Subject>(describing: Subject)
Creates a string representing the given value.
init<Subject>(describing: Subject)
Creates a string representing the given value.
init<Subject>(describing: Subject)
Creates a string representing the given value.
init<Subject>(describing: Subject)
Creates a string representing the given value.
init<Subject>(reflecting: Subject)
Creates a string with a detailed representation of the given value, suitable for
debugging.
init(contentsOfFile: String)
func append(Character)
Appends the given character to the string.
func append<S>(contentsOf: S)
Appends the characters in the given sequence to the string.
func append<S>(contentsOf: S)
Adds the elements of a sequence or collection to the end of this collection.
func reserveCapacity(Int)
Reserves enough space in the stringʼs underlying storage to store the specified number
of ASCII characters.
func removeFirst(Int)
Removes the specified number of elements from the beginning of the collection.
func removeLast(Int)
Removes the specified number of elements from the end of the collection.
func removeSubrange(Range<String.Index>)
Removes the characters in the given range.
func removeSubrange(Range<Index>)
Removes the elements in the specified subrange from the collection.
func removeSubrange<R>(R)
Removes the elements in the specified subrange from the collection.
Comparing Strings Comparing strings using the equal-to operator (==) or a relational operator (like < and >=) is
Using Operators always performed using the Unicode canonical representation, so that different
representations of a string compare as being equal.
init(String.UnicodeScalarView)
Creates a string corresponding to the given collection of Unicode scalars.
init(Substring.UnicodeScalarView)
Creates a String having the given content.
init(String.UTF16View)
Creates a string corresponding to the given sequence of UTF-16 code units.
init?(Substring.UTF16View)
Creates a String having the given content.
init(String.UTF8View)
Creates a string corresponding to the given sequence of UTF-8 code units.
init?(Substring.UTF8View)
Creates a String having the given content.
init(from: Decoder)
Creates a new instance by decoding from the given decoder.
init(NSString)
init(stringInterpolation: DefaultStringInterpolation)
Creates a new instance from an interpolated string literal.
init(stringLiteral: String)
Creates an instance initialized to the given string value.
init(unicodeScalarLiteral: String)
Creates an instance initialized to the given value.
init(extendedGraphemeClusterLiteral: String)
Creates an instance initialized to the given value.
func withContiguousStorageIfAvailable<R>((UnsafeBuffer
Pointer<Character>) -> R) -> R?
Call body(p), where p is a pointer to the collectionʼs contiguous storage. If no such
storage exists, it is first created. If the collection does not support an internal
representation in a form of contiguous storage, body is not called and nil is returned.
Reference Types Use bridged reference types when you need reference semantics or Foundation-specific
behavior.
class NSString
A static, plain-text Unicode string object that bridges to String; use NSString when
you need reference semantics or other Foundation-specific behavior.
class NSMutableString
A dynamic plain-text Unicode string object, for use instead of a String variable in
cases that require reference semantics.
protocol StringProtocol
A type that can represent a string as a collection of characters.
struct String.Index
A position of a character or code unit in a string.
struct String.UnicodeScalarView
A view of a stringʼs contents as a collection of Unicode scalar values.
struct String.UTF16View
A view of a stringʼs contents as a collection of UTF-16 code units.
struct String.UTF8View
A view of a stringʼs contents as a collection of UTF-8 code units.
struct String.Iterator
A type that provides the collectionʼs iteration interface and encapsulates its iteration
state.
struct String.Encoding
typealias String.SubSequence
A sequence that represents a contiguous subrange of the collectionʼs elements.
typealias String.CompareOptions
typealias String.EncodingConversionOptions
typealias String.EnumerationOptions
typealias String.IndexDistance
A type that represents the number of steps between two String.Index values, where
one value is reachable from the other.
typealias String.UnicodeScalarIndex
The index type for a stringʼs unicodeScalars view.
typealias String.ExtendedGraphemeClusterLiteralType
A type that represents an extended grapheme cluster literal.
typealias String.Indices
A type that represents the indices that are valid for subscripting the collection, in
ascending order.
typealias String.StringInterpolation
The type each segment of a string literal containing interpolations should be appended
to.
typealias String.StringLiteralType
A type that represents a string literal.
typealias String.UnicodeScalarLiteralType
A type that represents a Unicode scalar literal.
func makeContiguousUTF8()
If this string is not contiguous, make it so. If this mutates the string, it will invalidate any
pre-existing indices.
Relationships
Conforms To BidirectionalCollection
CKRecordValueProtocol
Comparable
CustomDebugStringConvertible
CustomReflectable
CustomStringConvertible
CVarArg
Decodable
Encodable
Equatable
ExpressibleByStringLiteral
Hashable
DocumentationSwift MLDataValueConvertible
String
Language: Swift MLIdentifier
API Changes: Show RangeReplaceableCollection
StringProtocol
TextOutputStream
TextOutputStreamable
See Also
struct Double
A double-precision, floating-point value type.
struct Array
An ordered, random-access collection.
struct Dictionary
A collection whose elements are key-value pairs.