String Literals
1 | let someString = "Some string literal value" |
Initializing an Empty String
1 | var emptyString = "" // empty string literal |
Find out whether a String value is empty by checking its Boolean isEmpty property:
1 | if emptyString.isEmpty { |
String Mutability
var
and let
1 | var variableString = "Horse" |
Strings Are Value Types
Swift’s String type is a value type.
Working with Characters
access the individual Character values for a String
1 | for character in "Dog!🐶".characters { |
Initialize a string with characters
1 | let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"] |
Concatenating Strings and Characters
String values can be added together
1 | let string1 = "hello" |
append a Character value to a String variable
1 | let exclamationMark: Character = "!" |
String Interpolation
1 | let multiplier = 3 |
Unicode
Swift’s native String type is built from Unicode scalar values
Special Characters in String Literals
- The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \” (double quote) and \’ (single quote)
- An arbitrary Unicode scalar, written as \u{n},
1 | let wiseWords = "\"Imagination is more important than knowledge\" - Einstein" |
Extended Grapheme Clusters
1 | let eAcute: Character = "\u{E9}" // é |
Counting Characters
The count of the characters returned by the characters property is not always the same as the length property of an NSString that contains the same characters. The length of an NSString is based on the number of 16-bit code units within the string’s UTF-16 representation and not the number of Unicode extended grapheme clusters within the string.
1 | let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪" |
Accessing and Modifying a String
String Indices
- 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. - If a String is empty, startIndex and endIndex are equal.
1 | let greeting = "Guten Tag!" |
Iterate indices
1 | let greeting = "Guten Tag!" |
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.
Inserting and Removing
1 | var welcome = "hello" |
You can use the 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.
Comparing Strings
String and Character Equality
- String and character equality is checked with the “equal to” operator (==) and the “not equal to” operator (!=)
- String and character comparisons in Swift are not locale-sensitive.
1 | let quotation = "We're a lot alike, you and I." |
Prefix and Suffix Equality
1 | let romeoAndJuliet = [ |
Unicode Representations of Strings
UTF-8 Representation
1 | for codeUnit in dogString.utf8 { |
UTF-16 Representation
1 | for codeUnit in dogString.utf16 { |
Unicode Scalar Representation
1 | for scalar in dogString.unicodeScalars { |