paragraph 형태로 string을 저장할 때는 “””를 쓰면 된다
만약 실제론 단락이 없었으면 할 때는 backslash()를 붙인다.
let softWrappedQuotation = """
The White Rabbit put on his spectacles. "Where shall I begin, \
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on \
till you come to the end; then stop."
"""
\"
(쌍따옴표) and \'
(따옴표) 표시할 때 사용
var emptyString = "" // empty string literal
var anotherEmptyString = String() // initializer syntax
// these two strings are both empty, and are equivalent to each other
if emptyString.isEmpty {
print("Nothing to see here")
}
// Prints "Nothing to see here"
isEmpty 프로퍼티로 string값이 있는지 없는지 확인한다
for character in "Dog!🐶" {
print(character)
} //string 내에서 for루프를 돌릴 수 있다
let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"]
let catString = String(catCharacters) //문자들을 가진 배열을 공백이 없는 string으로 바꿔준다
print(catString)
// Prints "Cat!🐱"
print(#"6 times 7 is \#(6 * 7)."#)
// Prints "6 times 7 is 42."
구획 문자를 쓴 경우, string interpolation을 사용할 때 #를 붙여준다
문자열의 문자 갯수를 알고 싶을 땐 count 프로퍼티를 이용한다
서로 다른 문자는 서로 다른 양의 메모리를 가지기 때문에 기본적으로 Swift는 string에서의 integer을 이용한 subscript를 지원하지 않는다
따라서 어떤 문자의 위치를 알고 싶을 땐 각각의 유니코드 스칼라를 처음부터 끝까지 톺아봐야 한다
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
startIndex는 문자열의 첫 번째 문자의 위치에 접근할 수 있게 해주지만 endIndex는 문자열의 마지막 문자 다음 위치에 접근하기 때문에 인덱싱 할 수 없다.
만약 비어있다면, startIndex = endIndex
+) (_:offsetBy:) 메서드를 통해 처음으로부터 원하는만큼 떨어져 있는 문자를 알 수 있다.
for index in greeting.indices {
print("\(greeting[index]) ", terminator: "")
}
//index를 print 하면 알아보지도 못 하는 메모리 값으로 나온다.
var welcome = "hello"
welcome.insert("!", at: welcome.endIndex)
// welcome now equals "hello!"
welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there!"
하나의 문자를 특정한 인덱스에 넣으려면 insert(_:at)
메서드를 쓰지만, 문자열일 경우 insert(contentsOf:at:)
메서드를 사용한다
welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome now equals "hello"
hasPrefix 와 hasSuffix 메서드로, 특정 접미사나 접두사가 있는지 확인할 수 있다
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"
]
var act1SceneCount = 0
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1 ") {
act1SceneCount += 1
}
}
print("There are \(act1SceneCount) scenes in Act 1")
// Prints "There are 5 scenes in Act 1"