TTS Swift Notes: Strings & Characters COPY Flashcards
What is a string literal?
A fixed sequence of textual character surrounded by a pair of double quotes (“”).
When do you use a string literal?
As an initial value for a constant or variable.
let someString = “Some string literal value”
How do you create an empty String?
Either assign an empty string literal to a variable or initialize a new String instance with initializer syntax.
- var emptyString = “” // empty string literal
- var anotherEmptyString = String() // initializer syntax
- // these two strings are both empty, and are equivalent to each other
What method checks a String’s Boolean to find out if it is empty?
.isEmpty
How do you indicate whether a particular String can be modified (or mutated)?
Assign it to a variable (in which case it can be modified) or to a constant (in which case it cannot be modified).
- 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
How do you access the individual character values for a string?
By iterating over its characters property with a for-in loop
1. for character in "Dog!🐶".characters { 2. print(character) 3. } 4. // D 5. // o 6. // g 7. // ! 8. // 🐶
You can add to a string value with what two operators?
- Addition operator (+)
2. Assignment operator (+=)
What method can add a value to a string variable?
.append
String interpolation is a way to do what?
To construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.
What is unicode?
An international standard for encoding, representing, and processing text in different writing systems.
What does unicode allow you to do?
To represent almost any character from any language in a standardized form, and to read and write those characters to and from an external source such as a text file or web page.
What is a unicode scalar?
A unique 21-bit number for a character or modifier, such as U+0061 for LATIN SMALL LETTER A(“a”).
What is the Unicode scalar range?
U+0000 to U+D7FF inclusive OR U+E000 to U+10FFFF inclusive.
What is an extended grapheme cluster?
A sequence of one or more Unicode scalars that (when combined) produce a single human-readable character. They area a flexible way to represent many complex script characters as a single Character value. EX: é
What method/property retrieves the count of the Character values in a string?
.count
Calling String.Index() does what?
Corresponds to the position of each Character in the string.