Unit 3: Strings Flashcards
Character
a single letter, number, punctuation mark, symbol or blank space
Escape character
\ -> avoids the normal behaviour of a string
Escape sequence
combines an escape character with another character or characters to achieve a new effect;
e.g.,
(\t) -> tab
(\n) -> new line
e.g.,
“Happy birthday, dear \ (name)”
Quick Look Button
an icon that resembles a small eye that gives users a sneak peek w/ more detail
String
text value
String interpolation
used when you’d like to place the value of a constant in the middle of a string literal
you add placeholders for constants or variables with an escape sequence that puts parentheses around the code to be replaced
e.g.,
“friendName = Lee”
“Have a good one, \ (friendName)”
=
“Have a good one, Lee”
Unicode
a character representation standard for any language
String
A text value
Characters
Of course, text can contain more than just letters: There are punctuation marks, numbers, symbols, even spaces. All of these things are known as characters, and strings are made up of characters.
Letters are characters:
Aa Bb Cc Dd Ee
So are punctuation marks:
! ? . , ; :
Numbers:
1 2 3 4 5 6 7 8 9 0
Symbols:
@ $ & © π * + - /
Even invisible characters like spaces and tabs:
(You can’t see them, but they’re there.)
Defining Strings
In the previous playground, you declared number constants like this:
let heightInCentimeters = 7817 //: To declare a string constant, you put text between quotation marks like this: let favoriteBook = "The Sun Also Rises"
let traditionalGreeting = "Hello, world!" //: The declared string values also appear in the results sidebar. 👉 //: - experiment: Practice by declaring `favoriteMovie` and `favoriteSong` string constants for your favorite movie and song: // Declare a favoriteMovie constant
// Declare a favoriteSong constant
Unicode
an international standard that can represent almost any character from any language in a standard way
Full
Strings in Swift are fully Unicode-compliant, so you can create strings that contain the text of different languages:
let englishGreeting = “Hello, World!”
let chineseGreeting = “你好,世界!”
let spanishGreeting = “¡Hola Mundo!”
let russianGreeting = “Привет мир!”
let japaneseGreeting = “こんにちは世界!”
//: Strings in different languages let you create apps that can be used by people around the world.
//:
//: Of course, programmers around the world speak different languages. In Swift, you can use Unicode in names:
// Constant name in Chinese that means ‘English Greeting’
let 英语问候 = “Hello, World!”
// Constant name in French that means 'English Greeting' let salutationAnglais = "Hello, World!" //: Emoji characters are also defined in Unicode, so you can include emoji in strings.\ //: (On the Mac, type Command-Control-Space to bring up an emoji picker.) let welcomingPhrase = "Welcome! 😀" //: You can also use emoji in names. That can be fun in small doses, but many programmers find it difficult to type, difficult to read, and less expressive than using words for names. let 🍓🍏🍒🍐🍋🍇 = "Fruit Salad" //: Move on to the next page to see how to combine strings together.
Combining Strings
Programmers often need to combine strings together.
For example, you might see a message like Chris likes your post in a social media app.
In Swift, you can combine strings by adding them together:
// This might change over time let username = "Chris"
// This part of the message will get reused let likesYourPostMessage = "likes your post"
// Combine strings by using the plus sign let finishedMessage = username + " " + likesYourPostMessage //: You can see how the strings have been combined in the results sidebar. 👉 //: //: What happens if you don’t add the space between the two strings? //: - experiment: Declare `firstName` and `lastName` string constants for your first and last name.\ //:Combine them into a `fullName` constant.\ //:Combine your `firstName` and `likesYourPostMessage`\ //:Type each line of code below the relevant comment below. // Declare a firstName constant
// Declare a lastName constant
// Combine the strings into a fullName constant
// Combine your first name with likesYourPostMessage
//: Next, find out how building strings by adding them can get complicated.
Fill in the Blanks
Combining strings by adding them together works very well. You may have noticed, however, that you needed to remember to add spaces between the words. Otherwise the words all run together:
// Forgot to add the space between the first name and last name. let fullName = "Johnny" + "Appleseed" //: //: Imagine an app that welcomes the user when they arrive in a new city. You want the string to say something like:\ //: _Hello Tara, welcome to Paris!_\ //: You could build that string by adding strings together, but it gets a little complicated: //: let firstName = "Tara" let city = "Paris" let welcomeString = "Hello " + firstName + ", welcome to " + city + "!" //: //: For building more complex strings like this, it would be useful to have a way to define a fill-in-the-blanks string. Something like this: //: //: “Hello \_\_\_\_\_\_\_, welcome to \_\_\_\_\_\_\_!” //: //: Swift has a way to do this. The official name for this is _string interpolation_. This sounds complicated and difficult, but it’s basically just fill-in-the-blanks. //: //: Move on to see how to do this in Swift. //:
String Interpolation
In Swift, you can define a string with placeholders that will be replaced with values. It works a lot like the fill-in-the-blanks example from the previous page:
“Hello _______, welcome to _______!”
You don’t use blank spaces as placeholders in Swift. You use (name). The value of name replaces the placeholder.
Here it is in action. Notice in the results sidebar that the values of firstName and city are filled in:
let firstName = "Tim" let city = "Cupertino"
let welcomeString = "Hello \(firstName), welcome to \(city)" //: - experiment: Create your own string describing your favorite food.\ //: “I like \_\_\_\_\_ because it is \_\_\_\_\_.” //: // Change this to your favorite food let favoriteFood = "pie"
// Change this to why you like it let reason = "tasty"
// Define a string below in the pattern “I like ___ because it is ___.”
//: On the next page, see what happens with long strings.
Viewing Playground Results
What if you define a long string in a playground? You’ll notice that the longer string is cut off at the end in the results sidebar. 👉
let spelledOutNumber = "six" let meal = "breakfast" let aliceQuotation = "Why, sometimes I’ve believed as many as \(spelledOutNumber) impossible things before \(meal)!" //: In the results sidebar, move the cursor over the line that begins “Why, sometimes I’ve believed…”. When you do, that result will be highlighted and two controls will appear: //: //: ![Sidebar Controls](SidebarControls.png) //: Click the control that looks like an eye. This is the QuickLook control. A popover will appear showing you more of the string value. //: //: Move the cursor over the control that looks like an outlined rectangle. This is the _show result_ button. If you click it, it will be highlighed and the result of that line of code will be added directly below the code. You can click this control again to hide the result. //: //: For now, leave the result of the long string showing. Change the values of the spelled out number and the meal. Notice how the result changes inline in the playground. //: //: On the next page, take these strings even further.