๐Ÿฆ… Swift

Strings and Characters

Kiwi๐Ÿ’ป 2022. 5. 22. 18:21

๋ฌธ์ž์—ด(String)์€ ๋ฌธ์ž(Character)์˜ ์—ฐ์†์ด๋‹ค. Swift์˜ String๊ณผ Character ํƒ€์ž…์€ ์‹ ์†ํ•˜๊ณ  ์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ ์ค€์ˆ˜ํ•œ๋‹ค. ๋ฌธ์ž์—ด์€ + ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒฐํ•ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋˜ํ•œ ๋ฌธ์ž์—ด ๋ณด๊ฐ„๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ์ƒ์ˆ˜,๋ณ€์ˆ˜,๋ฆฌํ„ฐ๋Ÿด ๋ฐ ํ‘œํ˜„์‹์„ ๊ธด ๋ฌธ์ž์—ด์— ์‚ฌ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ฑฐ๋‚˜, ์ €์žฅ ,์ถœ๋ ฅ์„ ์œ„ํ•œ ์ปค์Šคํ…€ ๋ฌธ์ž์—ด ๊ฐ’์„ ์‰ฝ๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์ž‡๋‹ค.

NOTEโ—๏ธ
Swift์˜ String ํƒ€์ž…์€ Foundation์˜ NSString ํด๋ž˜์Šค์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. Foundation์„ import ํ•˜๋ฉด ์บ์ŠคํŒ… ์—†์ด String ์—์„œ NSString ๋ฉ”์„œ๋“œ๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด (String Literals)

์ฝ”๋“œ์•ˆ์— ๋ฏธ๋ฆฌ ์„ ์–ธ๋œ String ๊ฐ’์— ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด (string literals)์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์Œ๋”ฐ์˜ดํ‘œ (")๋กœ ๋‘˜๋Ÿฌ์Œ“์ธ ๋ฌธ์ž์˜ ์—ฐ์†์„ ๋œปํ•œ๋‹ค.

์—ฌ๋Ÿฌ์ค„ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด (Multiline String Literals)

์—ฌ๋Ÿฌ์ค„์˜ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด 3๊ฐœ์˜ ์Œ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

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."
"""

์†Œ์Šค์ฝ”๋“œ์—์„œ ์—ฌ๋Ÿฌ์ค„ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์— ์ค„๋ฐ”๊ฟˆ์„ ํฌํ•จํ•˜๋ฉด ์ค„๋ฐ”๊ฟˆ๋„ ๋ฌธ์ž์—ด์˜ ๊ฐ’์œผ๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค. ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์‰ฝ๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ ์ค„๋ฐ”๊ฟˆ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€๋งŒ ์ค„๋ฐ”๊ฟˆ์ด ๋ฌธ์ž์—ด์˜ ๊ฐ’์˜ ์ผ๋ถ€๊ฐ€ ๋˜๋Š”๊ฑธ ์›์น˜ ์•Š์„ ๋•Œ๋Š” ์ค„ ๋ฐ”๊ฟˆ์„ ์›ํ•˜๋Š” ๋ผ์ธ ๋์— ๋ฐฑ์Šฌ๋ž˜์‹œ()๋ฅผ ์“ฐ๋ฉด ๋œ๋‹ค.

๋“ค์—ฌ์“ฐ๊ธฐ๋Š” ๋‹ซ๋Š” ๋”ฐ์˜ดํ‘œ๋ฅผ(""")๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ถ”๊ฐ€๋œ๋‹ค.

ํ™•์žฅ๋œ ๋ฌธ์ž์—ด ๊ตฌ๋ถ„๊ธฐํ˜ธ (Extended String Delimiters)

์ƒต ๊ธฐํ˜ธ(#)์œผ๋กœ ๋ฌธ์ž์—ด ์•ž๋’ค๋ฅผ ๊ฐ์‹ธ๋ฉด ๋‚ด๋ถ€์˜ ๋ฌธ์ž์—ด์„ ๊ธ€์ž ๊ทธ๋Œ€๋กœ ์ธ์‹ํ•œ๋‹ค. (RawString) ๋กœ์ŠคํŠธ๋ง์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ ํŠน์ˆ˜ ์˜ํ–ฅ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ํŠน์ˆ˜๋ฌธ์ž ๋‹ค์Œ์— ์ƒต์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋นˆ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™” (Initializing an Empty String)

๋นˆ String ๊ฐ’์„ ๋งŒ๋“ค๋ ค๋ฉด ๋นˆ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์œผ๋กœ ์ƒˆ String ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

var emptyString = ""               // empty string literal
var anotherEmptyString = String()  // initializer syntax
// these two strings are both empty, and are equivalent to each other

String ๊ฐ’์€ ๋ถ€์šธ isEmpty ํ”„๋กœํผํ‹ฐ๋กœ ๋น„์–ด์žˆ๋Š”์ง€ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์ž์—ด์€ ๊ฐ’ ํƒ€์ž… (Strings Are Value Types)

Swift์˜ String ํƒ€์ž…์€ ๊ฐ’ ํƒ€์ž… (value type) ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด String ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ณ  ์ด ๊ฐ’์„ ํ•จ์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•˜๋ ค๊ณ  ํ• ๋•Œ๋‚˜ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜์— ๋Œ€์ž…๋  ๋•Œ ๋ณต์‚ฌ๋˜์–ด ํ• ๋‹น๋œ๋‹ค.

String ๊ฐ’์„ ๋ณต์‚ฌํ•  ๋•Œ๋Š” ์ถœ์ฒ˜์™€ ๊ด€๊ณ„์—†์ด ์ •ํ™•ํ•œ ๊ฐ’์„ ๋ณด์žฅํ•œ๋‹ค. ๊ฒฐ๊ตญ ๊ฐ’์„ ์ง์ ‘ ์ˆ˜์ •ํ•˜์ง€ ์•Š๋Š”ํ•œ ์ˆ˜์ •๋˜์ง€ ์•Š๋Š”๋‹ค.

๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž ์—ฐ๊ฒฐ (Concatenating Strings and Characters)

  • String ๊ฐ’์€ ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์ž (+)๋กœ ํ•จ๊ป˜ ์ถ”๊ฐ€ (๋˜๋Š” ์—ฐ๊ฒฐ) ํ•˜์—ฌ ์ƒˆ๋กœ์šด String ๊ฐ’์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋˜ํ•œ ์กด์žฌํ•˜๋Š” String ๋ณ€์ˆ˜์— ๋ง์…ˆ ๋Œ€์ž… ์—ฐ์‚ฐ์ž (+=)๋กœ String ๊ฐ’์„ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  • String ํƒ€์ž…์˜ append() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ String ๋ณ€์ˆ˜์— Character ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์ž์—ด ์ ‘๊ทผ๊ณผ ์ˆ˜์ • (Accessing and Modifying a String)

๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ ๋˜๋Š” ์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ ๊ตฌ๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์— ์ ‘๊ทผ, ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์ž์—ด ์ธ๋ฑ์Šค(String Indices)

String ๊ฐ’์€ ์ธ๋ฑ์Šค ํƒ€์ž… (index type)๊ณผ ๊ด€๋ จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— , ๋‚ดํฌํ•˜๊ณ  ์žˆ๋Š” Character์˜ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” String.Index๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ๊ฐ์˜ ๋ฌธ์ž๋งˆ๋‹ค ์ €์žฅํ•  ๋ฉ”๋ชจ๋ฆฌ์˜ ์–‘์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ • ์œ„์น˜์˜ Charactor๋ฅผ ํ™•์ธ ํ•˜๋ ค๋ฉด String์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๊ฐ ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ๋ฅผ ๋ฐ˜๋ณตํ•ด์•ผ๋งŒ ํ•œ๋‹ค.

String ์— ์ฒซ๋ฒˆ์งธ Character ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” startIndex ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. endIndex ํ”„๋กœํผํ‹ฐ๋Š” String ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์˜ ๋‹ค์Œ์— ์œ„์น˜ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ endIndex ํ”„๋กœํผํ‹ฐ๋Š” ๋ฌธ์ž์—ด์˜ ์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ์— ์œ ํšจํ•œ ์ธ์ž๊ฐ€ ์•„๋‹ˆ๋‹ค. String๊ฐ’์ด ๋น„์–ด์žˆ๋‹ค๋ฉด startIndex ์™€ endIndex ๋Š” ๊ฐ™๋‹ค.

String์˜ ๋ฉ”์„œ๋“œ index(before:) ์™€ index(after:) ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์˜ ์ „๊ณผ ํ›„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์—์„œ ๋–จ์–ด์ง„ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์‹  index(_:offsetBy:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

๋ฌธ์ž์—ด ๋ฒ”์œ„์— ๋ฒ—์–ด๋‚˜๋Š” ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

greeting[greeting.endIndex] // Error
greeting.index(after: greeting.endIndex) // Error

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

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

์‚ฝ์ž…๊ณผ ์‚ญ์ œ (Inserting and Removing)

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

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!"

๋ฌธ์ž์—ด ํŠน์ • ์ธ๋ฑ์Šค์— ์žˆ๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์ž(Charater)๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด remove(at:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ํŠน์ • ๋ฒ”์œ„์˜ ๋ฌธ์ž์—ด์„ ์‚ญ์ œํ•˜๋ ค๋ฉด removeSubrange(_:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

NOTEโ—๏ธ
RangeReplaceableCollection ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋Š” ๋ชจ๋“  ํƒ€์ž…์—์„œ insert(:at:), insert(contentsOf:at:), remove(at:), removeSubrange(:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. String ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ Array, Dictionary,Set๊ณผ ๊ฐ™์€ ์ฝœ๋ ‰์…˜ ํƒ€์ž…๋„ ํฌํ•จ๋œ๋‹ค.

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

์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋‚˜ prefix(_:)๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์–ป์œผ๋ ค๊ณ  ํ• ๋•Œ ๊ทธ ๊ฐ’์€ String์ด ์•„๋‹Œ SubString ๊ฐ’์ด ๋œ๋‹ค. ์Šค์œ„ํ”„ํŠธ์—์„œ SubString์€ String์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฑฐ์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค(StringProtocol์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ). ์ด๊ฒƒ์€ ๋„ˆ๊ฐ€ subString์„ ์ด์šฉํ•˜์—ฌ String๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ SubString์€ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ๋งŒ ์‚ฌ์šฉ์„ ํ•˜๊ณ  ์ž‘์—…์ด ๋๋‚˜๋ฉด String์ธ์Šคํ„ด์Šค๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

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

// Convert the result to a String for long-term storage.
let newString = String(beginning)

SubString์—๋„ ๊ธฐ์กด String๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ €์žฅ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์ด ์กด์žฌํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ String๊ณผ SubString์˜ ์ฐจ์ด์ ์€ ๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด SubString์ด ์ €์žฅ๋  ๋•Œ ๊ธฐ์กด์˜ String์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ผ๋ถ€๋ฅผ ์žฌ์‚ฌ์šฉ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

  • newString์— ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ํ• ๋‹นํ• ๋•Œ ๊ณ ์œ ํ•œ ๋ฉ”๋ชจ๋ฆฌ์˜์—ญ์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

'๐Ÿฆ… Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Functions  (0) 2022.06.01
Control Flow  (0) 2022.06.01
Collection Types  (0) 2022.05.22
Basicย Operators  (0) 2022.05.15
The Basics  (0) 2022.05.15