Swift

๋ฌธ์ž์—ด ๋ฐ ๋ฌธ์ž ( Strings and Characters )

๊ธฐ๊ฐ€์ •ํ›ˆ 2022. 7. 17. 22:52

์•ˆ๋…•ํ•˜์„ธ์š” : ) 

๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž์— ๊ด€ํ•˜์—ฌ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

๐Ÿ“š์ฐธ๊ณ  ๋ฌธ์„œ ๋ฐ ๋งํฌ

 

Strings and Characters — The Swift Programming Language (Swift 5.7)

Strings and Characters A string is a series of characters, such as "hello, world" or "albatross". Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values. Swi

docs.swift.org


 

Swift์˜ String์€ Foundation์˜ ํ”„๋ ˆ์ž„์›Œํฌ์˜ NSString์ด bridge ๋œ ํƒ€์ž… ์ด๊ธฐ ๋•Œ๋ฌธ์— NSString์˜ ๋ฉ”์†Œ๋“œ๋ฅผ String์—์„œ ์บ์ŠคํŒ… ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด

๋ฌธ์ž์—ด์€ "   "๋กœ ๋ฌถ์–ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

let something = "Some string literal value"

 

์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด

let quotation = """
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."
"""

์—ฌ๋Ÿฌ ์ค„์˜ ๋ฌธ์ž์—ด์€ """    """ ๋กœ ๋ฌถ์–ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

//์—ฌ๋Ÿฌ์ค„ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๋ฉฐ ์ค„๋ฐ”๊ฟˆ์„ ํ• ๋ ค๋ฉด \ ( ๋ฐฑ์Šฌ๋ž˜์‰ฌ ) ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
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."
"""

//๋ฌธ์ž์—ด์˜ ์‹œ์ž‘๊ณผ ๋์— ๋นˆ์ค„์„ ๋„ฃ์„๋ ค๋ฉด ํ•œ ์ค„์„ ๋„์–ด์„œ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
let lineBreaks = """

This string starts with a line break.
It also ends with a line break.

"""

 

 

 

์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด์€ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“ค์—ฌ ์“ฐ๊ธฐ ๊ธฐ์ค€์€ ๋๋‚˜๋Š” ์ง€์ ์˜ """ ์˜ ์œ„์น˜์ž…๋‹ˆ๋‹ค.

๋‹ซ๋Š” """ ์œ„์น˜ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž๋“ค์€ ์ „๋ถ€ ๋ฌด์‹œ๋˜๊ณ  ๊ทธ ์ดํ›„์˜ ๊ณต๋ฐฑ์€ ๋ฌธ์ž์—ด์— ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค.

 

 

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์˜ ํŠน์ˆ˜ ๋ฌธ์ž

  • \0         : null ๋ฌธ์ž
  • \\          : ๋ฐฑ ์Šฌ๋ž˜์‹œ
  • \t          : ์ˆ˜ํ‰ ํƒญ
  • \n         : ์ค„ ๋ฐ”๊ฟˆ
  • \r          : ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด
  • \"         : ํฐ ๋”ฐ์˜ดํ‘œ
  • \'          : ์ž‘์€๋”ฐ์˜ดํ‘œ
  • \u{n}    : ์œ ๋‹ˆ์ฝ”๋“œ - n์€ 1-8์ž๋ฆฌ 16์ง„์ˆ˜ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

๋นˆ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™”

var emtpryString = ""
var anotherEmptyString = String()

๋ฌธ์ž์—ด์ด ๋น„์–ด์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” isEmpty๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

if emptyString.isEmpty {
    print("Nothing to see here")
}
//"Nothing to see here"

 

 

๊ฐ’ ํƒ€์ž… ๋ฌธ์ž์—ด

Swift์˜ String์€ ๊ฐ’ ํƒ€์ž…(value type)์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ String์ด ๋‹ค๋ฅธ ํ•จ์ˆ˜ ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ๋ถ€ํ„ฐ ๊ฐ’์ด ํ• ๋‹น๋  ๋•Œ ์ด์ „ String์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ฐ’์„ ๋ณต์‚ฌํ•ด์„œ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 

๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ์—์„œ ํ• ๋‹น๋ฐ›์€ ๋ฌธ์ž์—ด์€ ๊ทธ ๋ฌธ์ž์—ด์„ ์ˆ˜์ •ํ•ด๋„ ์›๋ณธ ๋ฌธ์ž์—ด์ด ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

๋‹ค์‹œ ๋ง ์“ฐ๊ธฐ

 

๋ฌธ์ž

๋ฌธ์ž์—ด์˜ ๊ฐœ๋ณ„ ๋ฌธ์ž๋ฅผ for-in loop์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

for character in "Cat!๐Ÿ˜ธ" {
    print(character)
}
// D
// o
// g
// !
// ๐Ÿ˜ธ

// ๋ฌธ์ž ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ• ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.
let exclamationMark: Character = "!"
let catCharacters: [Character] = ["D", "o", "g", "!", "๐Ÿถ"]
let catString = String(catCharacters)
print(catString)
// Prints "Dog!๐Ÿถ"

๋ฌธ์ž ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ์— ์ธ์ž๋กœ ๋„ฃ์–ด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž์˜ ๊ฒฐํ•ฉ

let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
print(welcome)
// "hello there"

var instruction = "look over"
instruction += string2
print(instruction)
//"look over there"

let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome : "hello there!"

 

๋ฌธ์ž์—ด ์‚ฝ์ž…

let mutiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message : "3 times 2.5 is 7.5"

๋ฐฑ์Šฌ๋ž˜์‹œ ๊ด„ํ˜ธ : \ ( ์ƒ์ˆ˜, ๋ณ€์ˆ˜, ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’ ) ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  -> \( )

 

๋ฌธ์ž์—ด์˜ ์ ‘๊ทผ๊ณผ ์ˆ˜์ •

๋ฌธ์ž์—ด์˜ ์ˆ˜์ •๊ณผ ์ ‘๊ทผ์€ ๋ฌธ์ž์—ด ๋ฉ”์†Œ๋“œ ํ˜น์€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ ์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”๋ณด๊ธฐ

์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ๋ž€ ์ฝœ๋ ‰์…˜, ๋ฆฌ์ŠคํŠธ, ์‹œํ€ธ์Šค ๋“ฑ ์ง‘ํ•ฉ์˜ ํŠน์ • ๋ฉค๋ฒ„ ์—˜๋ฆฌ๋จผํŠธ์— ๊ฐ„๋‹จํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ถ”๊ฐ€์ ์ธ ๋ฉ”์†Œ๋“œ ์—†์ด ํŠน์ • ๊ฐ’์„ ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ ๊ด€๋ จํ•ด์„œ๋Š” ๋”ฐ๋กœ ๊ธ€์„ ์ž‘์„ฑํ•˜์—ฌ ์ •๋ฆฌํ•ด๋ณผ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!!   ( ๋งํฌ ๋„ฃ์„ ๊ณณ )

๋ฌธ์ž์—ด ์ธ๋ฑ์Šค

  • startIndex                    : ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
  • endIndex                     : ๋ฌธ์ž์—ด์ด ๋๋‚œ ํ›„ ๋ฐ”๋กœ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜ โ€ป ๊ทธ๋ƒฅ ์ ‘๊ทผ ์‹œ out of bounds ์—๋Ÿฌ ๋ฐœ์ƒ  -> ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค!
  • index(before: )            : ์ง์ „ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
  • index(after: )               : ์งํ›„ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
  • index(_: offsetBy: )    :  offsetBy ๋ฉ”์†Œ๋“œ๋Š” ์‹œ์ž‘ ์ง€์ ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ์ •์ˆ˜ ๊ฐ’๋งŒ์„ ๋”ํ•œ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

โ–ถ  ์œ„์˜ ๋ฉ”์†Œ๋“œ๋“ค์€ Collection ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅด๋Š” Array, Dictionary, Set ๋“ฑ์—์„œ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„์˜ ๋ฉ”์†Œ๋“œ๋“ค์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค

let greeting = "Swift is good"
greeting[greeting.startIndex]
// greeting ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
// S

greeting[greeting.index(before: greeting.endIndex)]
// greeting์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
// d

greeting[greeting.index(after: greeting.startIndex)]
// greeting ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ’์˜ ์ดํ›„ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
// w

let index1 = greeting.index(greeting.startIndex, offsetBy: 0)	//S
greeting[index1]
//greeting.startIndex์œผ๋กœ ์‹œ์ž‘ ์‹œ์ ์„ ์ •ํ•˜๊ณ  0๋งŒํผ ๋–จ์–ด์ง„ ๊ฒƒ์€ ์ž๊ธฐ ์ž์‹ ์ด ๋ฉ๋‹ˆ๋‹ค. 

let index2 = greeting.index(greeting.startIndex, offsetBy: 4)	//t
greeting[index2]
//greeting.startIndex์œผ๋กœ ์‹œ์ž‘ ์‹œ์ ์„ ์ •ํ•˜๊ณ  4๋งŒํผ ๋–จ์–ด์ง„ ๊ฒƒ์€ t์ž…๋‹ˆ๋‹ค

let index3 = greeting.index(greeting.endIndex, offsetBy: -1)	//d
greeting[index3]
//greeting.endIndex์€ ์ „์ฒด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ ํ•˜๋ฏ€๋กœ -1์„ ๋”ํ•˜๋ฉด ์ „์ฒด String์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์ธ d๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

let index4 = greeting.index(greeting.endIndex, offsetBy: -4)	//g
greeting[index4]
//์œ„์— ๊ฐ™์€ ์›๋ฆฌ๋กœ -3์„ ๋”ํ•˜๋ฉด g๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

startIndex, endIndex๋Š” ์‹œ์ž‘๊ณผ ๋ ๊ฐ’๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์„œ ์ค‘๊ฐ„์— ์žˆ๋Š” ๋ฌธ์ž๋Š” ์ ‘๊ทผํ•˜๊ธฐ ์–ด๋ ค์›Œ index(_: offsetBy : )๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

โ€ป  endIndex์„ ์‚ฌ์šฉํ•ด์„œ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ ์˜ˆ์‹œ ( out of bounds )

greeting[greeting.endIndex] // ์—๋Ÿฌ!
greeting.index(after: greeting.endIndex) // ์—๋Ÿฌ!

greeting.startIndex๋Š” 0์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ greeting.endIndex๋Š” ์ „์ฒด String์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ 13์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ฒ”์œ„์— ๋ฒ—์–ด๋‚˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค. 

๋‘ ๋ฒˆ์งธ ์—๋Ÿฌ์ธ greeting.index(after: greeting.endIndex) -> endIndex์˜ after ๋˜ํ•œ ๋ฒ”์œ„์— ๋ฒ—์–ด๋‚˜ ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.

๋ฉ”์†Œ๋“œ ์˜ˆ์ œ์˜€๋˜ greeting[greeting.index(before: greeting.endIndex)] -> String ์ „์ฒด ๊ธธ์ด์˜ ์ด์ „์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ์ ์ธ ๊ฐ’์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

 

๋ฌธ์ž์—ด์˜ ๊ฐœ๋ณ„ ๋ฌธ์ž๋ฅผ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” indices ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

for index in greeting.indices {
    print("\(greeting[index]) ", terminator: "")
// G u t e n  T a g !
}

↓ terminator์— ๊ด€ํ•˜์—ฌ 

๋”๋ณด๊ธฐ
print ํ•จ์ˆ˜์˜ terminator , separator ๊ด€๋ จ ๋ฌธ์„œ
๊ณต์‹ ๋ฌธ์„œ์ธ Apple Developer Documentation printํ•จ์ˆ˜์—์„œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ๋ณผ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค!

๊ณต๋ฐฑ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ํ•ญ๋ชฉ์œผ๋กœ ๊ตฌ๋ถ„๋œ ํ•ญ๋ชฉ์„ ์ธ์‡„ํ•˜๋ ค๋ฉด ๋ฌธ์ž์—ด์€ separator
print(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
// Prints "1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0"

// separator์„ ์‚ฌ์šฉ ์•ˆ ํ–ˆ์„ ๊ฒฝ์šฐ
print(1.0, 2.0, 3.0, 4.0, 5.0)
// 1.0 2.0 3.0 4.0 5.0

๊ฐ ํ˜ธ์ถœ์˜ ์ถœ๋ ฅ์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐœํ–‰์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ํ›„ํ–‰ ๊ฐœํ–‰ ์—†์ด ํ•ญ๋ชฉ์„ print ํ•˜๋ ค๋ฉด ๋นˆ ๋ฌธ์ž์—ด์€ terminator

for n in 1...5 {
    print(n, terminator: "")
}
// Prints "12345"

// terminator ์‚ฌ์šฉ ์•ˆ ํ–ˆ์„ ๊ฒฝ์šฐ
for n in 1...5 {
    print(n)
}
// 1
// 2
// 3
// 4
// 5

 

 

 

๋ฌธ์ž์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ

๋ฌธ์ž์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ ๋ฉ”์†Œ๋“œ์—๋Š”

  • insert ( :at: )                        : ํ•ด๋‹น ์œ„์น˜์— ์›์†Œ ์‚ฝ์ž…
  • insert ( contentsOf: at : )   : ํ•ด๋‹น ์œ„์น˜์— ์›์†Œ ์‚ฝ์ž…
  • remove( at: )                       : ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ์›์†Œ ์ œ๊ฑฐ
  • removeSubrange( : )          : ํŠน์ • ์œ„์น˜์— ์›์†Œ ์ œ๊ฑฐ

โ–ถ  ์œ„์˜ ๋ฉ”์†Œ๋“ค์€ RangeReplaceableCollection ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅด๋Š” Array, Dictionary, Set ๋“ฑ์—์„œ๋„ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”๋ณด๊ธฐ

RangeReplaceableCollection ํ”„๋กœํ† ์ฝœ ๊ด€๋ จํ•ด์„œ๋Š” ๋”ฐ๋กœ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค!!

var welcome = "hello"
welcome.insert("!", at: welcome.endIndex)
// welcome ์˜ ๋ฌธ์ž์—ด์ด ๋๋‚œ ๋ฐ”๋กœ ๋’ค์— ์ถ”๊ฐ€๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.
// welcome : hello!

welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))
// welcome์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’ ์•ž์— there ๊ฐ’์„ insert ํ•ด์ค๋‹ˆ๋‹ค.
// 2๋ฒˆ ๊ตฌ๋ฌธ์—์„œ welcome์„ var์œผ๋กœ ์„ ์–ธํ•˜์—ฌ "hello" -> "hello!"์œผ๋กœ ๋ฐ”๋€Œ์–ด ์•„๋ž˜ ๊ฒฐ๊ณผ ๊ฐ’์œผ๋กœ ์ถœ๋ ฅ 
// welcome : hello there!

//
var goodbye = "hi swift@"
goodbye.insert(contentsOf: " there", at: goodbye.index(before: goodbye.endIndex))
// welcome : hi swift there@

๋ฌธ์ž์—ด์˜ ํŠน์ • ์ธ๋ฑ์Šค๋กœ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด insert ( :at: )  ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํŠน์ • ์ธ๋ฑ์Šค์— ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์„ ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด insert ( contentsOf: at : )๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

var welcomeRemove = "hello there!"

welcomeRemove.remove(at: welcomeRemove.index(before: welcomeRemove.endIndex))
// ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ ์ง€์›๋‹ˆ๋‹ค.
// welcomeRemove : hello there


var welcome = "hello there!"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome์˜ String๊ฐ’์—์„œ -6์„ ๋บธ ์ง€์ ์—์„œ welcome์˜ ๋งˆ์ง€๋ง‰ ์ง€์ ๊นŒ์ง€ let range๋กœ ์„ ์–ธ
// removeSubrange์—์„œ range๋ถ€๋ถ„์„ ์ œ๊ฑฐ
// welcome : hello

ํŠน์ • ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋ฌธ์ž์—ด์—์„œ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด remove( at: ) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํŠน์ • ๋ฒ”์œ„์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์„œ๋ธŒ ์ŠคํŠธ๋ง์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด removeSubrange( : ) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

๋ถ€๋ถ„ ๋ฌธ์ž์—ด ( Substrings )

๋ฌธ์ž์—ด์—์„œ ๋ถ€๋ถ„ ๋ฌธ์ž๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด prefix (_ : ) ์™€ ๊ฐ™์€ ์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ์–ป์€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ๋ฌธ์ž์—ด ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค.

โ–ท  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋‹จ๊ธฐ๊ฐ„์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์•„๋‹Œ ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋™์•ˆ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ฌธ์ž์—ด ์ธ์Šคํ„ด์Šค๋กœ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.

let greeting = "Hello, World!"
let index = greeting.index(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning : Hello

// SubString์ธ beginning์„ String์œผ๋กœ ๋ณ€ํ™˜
let newString = String(beginning)

์œ„์˜ ์˜ˆ์ œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด์•ผ ์ข‹์€ ์ด์œ ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ์™€ ์—ฐ๊ด€ ์žˆ์Šต๋‹ˆ๋‹ค. Substring์€ ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ง์ ‘ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์›๋ณธ String์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

SubString์„ ๊ณ„์† ์ด์šฉํ•˜๋Š” ์ด์ƒ์€ ์›๋ณธ String์ธ greeting์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๊ณ„์† ๋‚จ์•„ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž์—ด๊นŒ์ง€๋„ ๋‚จ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— SubString์„ ์˜คํ•ด ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์œ„์˜ ์ฝ”๋“œ ์˜ˆ์ œ์ฒ˜๋Ÿผ String์—์„œ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค๋†“๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ฆฌ ํšจ์œจ๋ฉด์—์„œ ์ข‹์Šต๋‹ˆ๋‹ค.

๋”๋ณด๊ธฐ

โ€ป String๊ณผ SubString ๋ชจ๋‘ StringProtocol์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๋ฌธ์ž ์กฐ์ž‘์— ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค

 

๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž ๋น„๊ต

๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž ๋น„๊ต์—๋Š” ==( ์ผ์น˜ ) ๋˜๋Š” !=( ๋ถˆ์ผ์น˜ ) ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

let quotation = "We're a lot alike, you and I."
let sameQuotation = "We're a lost alike, you and I."
if quotation == sameQuotation {
    print("These two strings are considered equal")
}
// quotation ๊ณผ sameQuotation ๋ฌธ์ž์—ด์ด ๊ฐ™์„ ๊ฒฝ์šฐ ์ถœ๋ ฅ
// These two strings are considered equal ์ถœ๋ ฅ

 

 

์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ ๋น„๊ต

์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ์˜ ๋น„๊ต๋ฅผ ์œ„ํ•ด hasPrefix ( : ) , hasSuffix( : ) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

String์ธ์ž๋ฅผ ๋ฐ›์•„ ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ Boolean๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

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 remeoAndJuliet {
    if scene.hasPrefix("Act 1 ") {
        act1SceneCount += 1
    }
}
print("There are \(act1SceneCount) scenes in Act 1")
// There are 5 scenes in Act 1

์œ„์˜ ์ฝ”๋“œ๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด์—์„œ ์ ‘๋‘์–ด Act 1์ด ๋ช‡ ๊ฐœ ๋“ค์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค

๋‹ค์Œ์˜ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด์—์„œ ์ ‘๋ฏธ์–ด Capulet's mansion๊ณผ Friar Lawrences' cell์ด ๊ฐ๊ฐ ๋ช‡ ๊ฐœ ๋“ค์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

var mansionCount = 0
var cellCount = 0
for scene in remeoAndJuliet {
    if scene.hasSuffix("Capulet's mansion") {
        mansionCount += 1
    } else if scene.hasSuffix("Friar Lawrence's cell") {
        cellCount += 1
    }
}
print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")
// 6 mansion scenes; 2 cell scenes

 


 

 

๋ฌธ์ž์—ด ๋ฐ ๋ฌธ์ž์— ๊ด€๋ จํ•˜์—ฌ ์ •๋ฆฌ๋ฅผ ํ–ˆ๋Š”๋ฐ์š” ์œ ๋‹ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ๋”ฐ๋กœ ๊ณต๋ถ€์™€ ์ •๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ธ€๋กœ ์ž‘์„ฑํ•ด ๋ณผ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!!