let quotation = """
to be
or not to be
that's a question
"""
let str1 = "they are same"
let str2 = """
they are same
"""
let a = """
to be, \
or not to be,\
that's a question
"""
//to be, or not to be, that's a question
let str = """
to be,
or not to be,
that's a question
"""
//to,
// or not to be,
//that's a question
var emptyString = ""
var anotherEmptyString = String()
if emptyString.isEmpty {
print("nothing to see here")
}
var variableString = "horse"
variableString += " and carriage";
when you pass a string value to a function or assign it to a constant or variable, a new copy of this existing string value is created, and the new copy is passed or assigned, not the original version
let greeting = "Guten tag!"
greeting[greeting.startIndex] //G
greeting[greeting.index(before: greeting.endIndex)]//!
greeting[greeting.index(after: greeting.startIndex)]//u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]//a
for index in greeting.indices {
print(greeting[index])
}
you can use the index methods on any type conforming Collection protocol, such as Array, Set, Dictionary
var welcome = "hello"
welcome.insert("!", at: welcome.endIndex)
//hello!
welcome.insert(contensOf: " there", at: welcome.index(before: welcome.endIndex))
//hello there!
welcome.remove(at: welcome.index(before: welcome.endIndex))
//hello there
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubstring(range)
//"hello"
you can use insert(:at:) insert(contentsOf:at:) remove(at:) removeSubstring( methods on any type conforming to the RangeReplaceableCollection protocol, such as Array, Set, Dictionary
let greeting = "hello, world!"
let index = greeting.firstIndex(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
//beginning = "hello"
let newString = String(beginning)
let quotation = "asdfzxcv"
let sameQuotation = "asdfzxcv"
if quotation == sameQuotation {
print("they are the same strings")
}
let romeoAndJuliet = [
"Act 1 Scene 1: Verona, A public place",
"Act 1 Scene 2: Capulet's mansion",
"Act 1 Scene 3: A room in Capulet's mansion",
"Act 1 Scene 4: A street outside Capulet's mansion",
"Act 1 Scene 5: The Great Hall in Capulet's mansion",
"Act 2 Scene 1: Outside Capulet's mansion",
"Act 2 Scene 2: Capulet's orchard",
"Act 2 Scene 3: Outside Friar Lawrence's cell",
"Act 2 Scene 4: A street in Verona",
"Act 2 Scene 5: Capulet's mansion",
"Act 2 Scene 6: Friar Lawrence's cell"
]
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1") {
print(scene)
}
}
for scene in romeoAndJuliet {
if scene.hasSuffix("Capulet's mansion") {
print(scene)
} else if scene.hasSuffix("Friar Lawrence's cell") {
print(scene)
}
}