TTS Swift Notes: Strings & Characters Flashcards

1
Q

What is a string literal?

A

A fixed sequence of textual character surrounded by a pair of double quotes (“”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When do you use a string literal?

A

As an initial value for a constant or variable.

let someString = “Some string literal value”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create an empty String?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What method checks a String’s Boolean to find out if it is empty?

A

.isEmpty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you indicate whether a particular String can be modified (or mutated)?

A

Assign it to a variable (in which case it can be modified) or to a constant (in which case it cannot be modified).

  1. var variableString = “Horse”
  2. variableString += “ and carriage”
  3. // variableString is now “Horse and carriage”
  4. let constantString = “Highlander”
  5. constantString += “ and another Highlander”

// this reports a compile-time error - a constant string cannot be modified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you access the individual character values for a string?

A

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. // 🐶
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

You can add to a string value with what two operators?

A
  1. Addition operator (+)

2. Assignment operator (+=)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What method can add a value to a string variable?

A

.append

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

String interpolation is a way to do what?

A

To construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is unicode?

A

An international standard for encoding, representing, and processing text in different writing systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does unicode allow you to do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a unicode scalar?

A

A unique 21-bit number for a character or modifier, such as U+0061 for LATIN SMALL LETTER A(“a”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Unicode scalar range?

A

U+0000 to U+D7FF inclusive OR U+E000 to U+10FFFF inclusive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an extended grapheme cluster?

A

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: é

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What method/property retrieves the count of the Character values in a string?

A

.count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Calling String.Index() does what?

A

Corresponds to the position of each Character in the string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can Swift string be indexed by integer values?

A

No

18
Q

What property should you use to access the first position Character of a String?

A

startIndex

19
Q

The endIndex property is what position of a String?

A

After the String’s last character.

20
Q

If a String is empty, startIndex and endIndex are what?

A

Equal

21
Q

Any index in a String can be accessed from any other index by chaining the predecessor(), successor() or this method.

A

advancedBy(_:)

22
Q

What method allows a String.Index value to access its immediately preceding index?

A

predecessor()

23
Q

What method allows a String.Index value to access its immediately succeeding index?

A

successor()

24
Q

What method inserts a character into a string at a specified index?

A

insert(_:atIndex:)

25
Q

What method inserts the contents of another string at a specified index?

A

insertContentsOf(_:at:)

26
Q

What method removes a character from a string at a specified index?

A

removeAtIndex(_:)

27
Q

What method removes a substring at a specified range?

A

removeRange(_:)

28
Q

What are the three ways that Swift provides to compare textual values?

A
  1. string and character equality
    1. prefix equality
    2. suffix equality
29
Q

What two operators check string and character equality?

A

The equal to operator (==)

The not equal to operator (!=)

30
Q

In Swift, are string and character comparisons locale-sensitive?

A

No

31
Q

What method checks if a string has a particular string prefix?

A

hasPrefix(_:)

32
Q

What method checks if a string has a particular string suffix?

A

hasSuffix(_:)

33
Q

hasPrefix(:) and hasSuffix(:) return what kind of value?

A

Boolean

34
Q

Encoding forms encode strings into small chunks called what?

A

Code units

35
Q

UTF-8 encoding form has how many code units?

A

8-bit code units.

36
Q

You can access a UTF-8 representation of a String by iterating over its what?

A

utf8 property

37
Q

The utf8 property is of what type?

A

String.UTF8View

38
Q

How do you access a Unicode scalar representation of a String value?

A

By iterating over its unicodeScalars property.

39
Q

The unicodeScalars property is of what type?

A

UnicodeScalarView

40
Q

UnicodeScalarView is a collection of what value type?

A

UnicodeScalar

41
Q

Each UnicodeScalar value can be used to do what?

A

Construct a new String value.