0% found this document useful (0 votes)
2 views16 pages

2 01 Strings

Uploaded by

Abhijeet Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

2 01 Strings

Uploaded by

Abhijeet Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Unit 2—Lesson 1:

Strings
Strings

let greeting = "Hello"


var otherGreeting = "Salutations"

let joke = """


Q: Why did the chicken cross the road?
A: To get to the other side!
"""
print(joke)

Q: Why did the chicken cross the road?


A: To get to the other side!
String basics
Escaping
let greeting = "It is traditional in programming to print \"Hello, world!\""

Escape Description

\" Double quote

\\ Backslash

\t Tab

\r Carriage return (return to beginning of the next line)


String basics
Empty strings
var myString = ""

if myString.isEmpty {
print("The string is empty")
}
String basics
Characters
let a = "a" // 'a' is a string
let b: Character = "b" // 'b' is a Character
Concatenation

let string1 = "Hello"


let string2 = ", world!"
var myString = string1 + string2 // "Hello, world!"

myString += " Hello!" // "Hello, world! Hello!"


Interpolation

let name = "Rick"


let age = 30
print("\(name) is \(age) years old")

Rick is 30 years old


Interpolation
Expressions
let a = 4
let b = 5
print("If a is \(a) and b is \(b), then a + b equals \(a+b)")

If a is 4 and b is 5, then a + b equals 9


String equality and comparison

let month = "January"


let otherMonth = "January"
let lowercaseMonth = "january"

if month == otherMonth {
print("They are the same")
}

if month != lowercaseMonth {
print("They are not the same.")
}

They are the same.


They are not the same.
String equality and comparison
Ignoring case
let name = "Johnny Appleseed"
if name.lowercased() == "joHnnY aPPleseeD".lowercased() {
print("The two names are equal.")
}

The two names are equal.


String equality and comparison
Prefix and suffix
let greeting = "Hello, world!"

print(greeting.hasPrefix("Hello"))
print(greeting.hasSuffix("world!"))
print(greeting.hasSuffix("World!"))

true
true
false
String equality and comparison
Finding substrings
let greeting = "Hi Rick, my name is Amy."
if greeting.contains("my name is") {
print("Making an introduction")
}

Making an introduction
String equality and comparison
Checking length
let name = "Ryan Mears"
let count = name.count
let newPassword = "1234"

if newPassword.count < 8 {
print("This password is too short. Passwords should have at least 8 characters.")
}

This password is too short. Passwords should have at least 8 characters.


String equality and comparison
Using switch
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
print("\(someCharacter) is a vowel.")
default:
print("\(someCharacter) is not a vowel.")
}

e is a vowel.
Unicode

let cow = "🐮"


let credentials = "résumé"
let myBook = "私の本"
print("∞".count)

1
© 2021 Apple Inc.
This work is licensed by Apple Inc. under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

You might also like