String concatenation is as simple as combining two strings with the +
operator, and string mutability is managed by choosing between a constant or a variable.Every string is composed of encoding-independent Unicode characters.
NOTE
Swift’s String
type is bridged with Foundation’s NSString
class. Foundation also extends String
to expose methods defined by NSString
. This means, if you import Foundation, you can access those NSString
methods on String
without casting.
String Literals
Use a string literal as an initial value for a constant or variable:
let someString = “Some string literal value”
Multiline String Literals
let quotation = “”“
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?” he asked
“Begin at the beginning,” the King said gravely, “and go on
till you come to the end; then stop.”
“"”
When your source code includes a line break inside of a multiline string literal, that line break also appears in the string’s value. If you want to use line breaks to make your source code easier to read, but you don’t want the line breaks to be part of the string’s value, write a backslash () at the end of those lines:
let softWrappedQuotation = ”“”
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?“ he asked.
"Begin at the beginning,” the King said gravely, “and go on
till you come to the end; then stop.”
“"”
To make a multiline string literal that begins or ends with a line feed, write a blank line as the first or last line. For example:
let lineBreaks = ”“”
This string starts with a line break.
It also ends with a line break.
“”“
A multiline string can be indented to match the surrounding code. The whitespace before the closing quotation marks ("""
) tells Swift what whitespace to ignore before all of the other lines. However, if you write whitespace at the beginning of a line in addition to what’s before the closing quotation marks, that whitespace is included.

Special Characters in String Literals
String literals can include the following special characters:
- The escaped special characters
(null character),
\
(backslash),t
(horizontal tab),n
(line feed),r
(carriage return),"
(double quotation mark) and'
(single quotation mark) - An arbitrary Unicode scalar, written as
u{
n}
, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point
Because multiline string literals use three double quotation marks instead of just one, you can include a double quotation mark ("
) inside of a multiline string literal without escaping it.
Initializing an Empty String
var emptyString = ”“ // empty string literal
var anotherEmptyString = String() // initializer syntax
// these two strings are both empty, and are equivalent to each other
Find out whether a String
value is empty by checking its Boolean isEmpty
property:
if emptyString.isEmpty {
print("Nothing to see here”)
}
// Prints “Nothing to see here”
String Mutability
var variableString = “Horse”
variableString += “ and carriage”
// variableString is now “Horse and carriage”
let constantString = “Highlander”
constantString += “ and another Highlander”
// this reports a compile-time error – a constant string cannot be modified
Strings Are Value Types
Swift’s String
type is a value type. If you create a new String
value, that String
value is copied when it’s passed to a function or method, or when it’s assigned to a constant or variable. In each case, a new copy of the existing String
value is created, and the new copy is passed or assigned, not the original version. (copy by value, not reference)
Working with Characters
for character in “Dog!?” {
print(character)
}
// D
// o
// g
// !
// ?
let exclamationMark: Character = “!”
캐릭터 배열을 통해 string 만드는법
let catCharacters: [Character] = [“C”, “a”, “t”, “!”, “?”]
let catString = String(catCharacters)
print(catString)
// Prints “Cat!?”
Concatenating Strings and Characters
let string1 = “hello”
let string2 = “ there”
var welcome = string1 + string2
// welcome now equals “hello there”
var instruction = “look over”
instruction += string2
// instruction now equals “look over there”
let exclamationMark: Character = “!”
welcome.append(exclamationMark)
// welcome now equals “hello there!”
NOTE
You can’t append a String
or Character
to an existing Character
variable, because a Character
value must contain a single character only.
String Interpolation
Each item that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash ():
let multiplier = 3
let message = “(multiplier) times 2.5 is (Double(multiplier) * 2.5)”
// message is “3 times 2.5 is 7.5”
Unicode
Swift’s String
and Character
types are fully Unicode-compliant.
Unicode Scalars
NOTE
A Unicode scalar is any Unicode code point in the range U+0000
to U+D7FF
inclusive or U+E000
to U+10FFFF
inclusive. Unicode scalars don’t include the Unicode surrogate pair code points, which are the code points in the range U+D800
to U+DFFF
inclusive.
Accessing and Modifying a String
You access and modify a string through its methods and properties, or by using subscript syntax.
String Indices
Each String
value has an associated index type, String.Index
, which corresponds to the position of each Character
in the string.
As mentioned above, different characters can require different amounts of memory to store, so in order to determine which Character
is at a particular position, you must iterate over each Unicode scalar from the start or end of that String
. For this reason, Swift strings can’t be indexed by integer values.
Use the startIndex
property to access the position of the first Character
of a String
. The endIndex
property is the position after the last character in a String
. As a result, the endIndex
property isn’t a valid argument to a string’s subscript. If a String
is empty, startIndex
and endIndex
are equal.
You access the indices before and after a given index using the index(before:)
and index(after:)
methods of String
. To access an index farther away from the given index, you can use the index(_:offsetBy:)
method instead of calling one of these methods multiple times.
let greeting = “Guten Tag!”
greeting[greeting.startIndex]
// G
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a
greeting[greeting.endIndex] // Error
greeting.index(after: greeting.endIndex) // Error
Use the indices
property to access all of the indices of individual characters in a string.
for index in greeting.indices {
print(“(greeting[index]) ”, terminator: “”)
}
// Prints “G u t e n T a g ! ”
NOTE
You can use the startIndex
and endIndex
properties and the index(before:)
, index(after:)
, and index(_:offsetBy:)
methods on any type that conforms to the Collection
protocol. This includes String
, as shown here, as well as collection types such as Array
, Dictionary
, and Set
.
(string이나 array나 dictionary나 set은 같은 방법으로 접근 가능하다는 이야기)
Inserting and Removing
insert(_:at:)
insert(contentsOf:at:)
method.
var welcome = “hello”
welcome.insert(“!”, at: welcome.endIndex)
// welcome now equals “hello!”
welcome.insert(contentsOf: “ there”, at: welcome.index(before: welcome.endIndex))
// welcome now equals “hello there!”
remove(at:)
removeSubrange(_:)
welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome now equals “hello there”let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome now equals “hello”
NOTE
You can use the insert(_:at:)
, insert(contentsOf:at:)
, remove(at:)
, and removeSubrange(_:)
methods on any type that conforms to the RangeReplaceableCollection
protocol. This includes String
, as shown here, as well as collection types such as Array
, Dictionary
, and Set
.
(string이나 array나 dictionary나 set은 같은 방법으로 접근 가능하다는 이야기)
Substrings
When you get a substring from a string—for example, using a subscript or a method like prefix(_:)
—the result is an instance of Substring
, not another string. Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings. However, unlike strings, you use substrings for only a short amount of time while performing actions on a string. When you’re ready to store the result for a longer time, you convert the substring to an instance of String
.
let greeting = “Hello, world!”
let index = greeting.index(of: “,”) ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning is “Hello”
// Convert the result to a String for long-term storage.
let newString = String(beginning)
NOTE
Both String
and Substring
conform to the StringProtocol
protocol, which means it’s often convenient for string-manipulation functions to accept a StringProtocol
value. You can call such functions with either a String
or Substring
value.
Comparing Strings
Swift provides three ways to compare textual values: string and character equality, prefix equality, and suffix equality.
String and Character Equality
String and character equality is checked with the “equal to” operator (==
) and the “not equal to” operator (!=
), as described in Comparison Operators:
let quotation = “We’re a lot alike, you and I.”
let sameQuotation = “We’re a lot alike, you and I.”
if quotation == sameQuotation {
print(“These two strings are considered equal”)
}
// Prints “These two strings are considered equal”
Two String
values (or two Character
values) are considered equal if their extended grapheme clusters are canonically equivalent. Extended grapheme clusters are canonically equivalent if they have the same linguistic meaning and appearance, even if they’re composed from different Unicode scalars behind the scenes.
Prefix and Suffix Equality
To check whether a string has a particular string prefix or suffix, call the string’s hasPrefix(_:)
and hasSuffix(_:)
methods, both of which take a single argument of type String
and return a Boolean value.
let romeoAndJuliet = [
"Act 1 Scene 1: Verona, A public place",
"Act 1 Scene 2: Capulet’s mansion",
"Act 1 Scene 3: A room in Capulet’s mansion",
"Act 1 Scene 4: A street outside Capulet’s mansion",
"Act 1 Scene 5: The Great Hall in Capulet’s mansion",
"Act 2 Scene 1: Outside Capulet’s mansion",
"Act 2 Scene 2: Capulet’s orchard",
"Act 2 Scene 3: Outside Friar Lawrence’s cell",
"Act 2 Scene 4: A street in Verona",
"Act 2 Scene 5: Capulet’s mansion",
"Act 2 Scene 6: Friar Lawrence’s cell"
]
var act1SceneCount = 0
for scene in romeoAndJuliet {
if scene.hasPrefix(“Act 1 ”) {
act1SceneCount += 1
}
}
print(“There are (act1SceneCount) scenes in Act 1”)
// Prints “There are 5 scenes in Act 1”
var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
if scene.hasSuffix(“Capulet’s mansion”) {
mansionCount += 1
} else if scene.hasSuffix(“Friar Lawrence’s cell”) {
cellCount += 1
}
}
print(“(mansionCount) mansion scenes; (cellCount) cell scenes”)
// Prints “6 mansion scenes; 2 cell scenes”
Unicode Representations of Strings
When a Unicode string is written to a text file or some other storage, the Unicode scalars in that string are encoded in one of several Unicode-defined encoding forms. Each form encodes the string in small chunks known as code units. These include the UTF-8 encoding form (which encodes a string as 8-bit code units), the UTF-16 encoding form (which encodes a string as 16-bit code units), and the UTF-32 encoding form (which encodes a string as 32-bit code units).
Swift provides several different ways to access Unicode representations of strings. You can iterate over the string with a for
–in
statement, to access its individual Character
values as Unicode extended grapheme clusters. This process is described in Working with Characters.
Alternatively, access a String
value in one of three other Unicode-compliant representations:
- A collection of UTF-8 code units (accessed with the string’s
utf8
property) - A collection of UTF-16 code units (accessed with the string’s
utf16
property) - A collection of 21-bit Unicode scalar values, equivalent to the string’s UTF-32 encoding form (accessed with the string’s
unicodeScalars
property)
(unicode라고 하더라도 하나의 character가 가지는 크기에 따라 위에 같이 나뉘게된다. 그렇지만 ascii가 차지하는것은 같은크기 )
let dogString = “Dog‼?”
for codeUnit in dogString.utf8 {
print(“(codeUnit) ”, terminator: “”)
}
print(“”)
// Prints “68 111 103 226 128 188 240 159 144 182 ”
for codeUnit in dogString.utf16 {
print(“(codeUnit) ”, terminator: “”)
}
print(“”)
// Prints “68 111 103 8252 55357 56374 ”
for scalar in dogString.unicodeScalars {
print(“(scalar.value) ”, terminator: “”)
}
print(“”)
// Prints “68 111 103 8252 128054 ”
for scalar in dogString.unicodeScalars {
print(“(scalar) ”)
}
// D
// o
// g
// ‼
// ?