Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift Documents

[Swift] ๊ณต์‹ ๋ฌธ์„œ ์ •๋ฆฌ (3) - Strings and Characters (๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž)

๊ฐ์ž ๐Ÿฅ” 2022. 1. 11. 14:56
๋ฐ˜์‘ํ˜•

์‹œ์ž‘ํ•˜๋ฉฐ

swift ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•œ์ง€ 3์ผ์ฐจ... ์š”์ฆ˜ ๊ณต๋ถ€๊ฐ€ ๋œธํ•˜๋‹ค. ๋‚ด ์†Œ์ค‘ํ•œ ์นœ๊ตฌ๊ฐ€ ํ•˜๋Š˜๋‚˜๋ผ๋กœ ๊ฐ„์ง€ ์ผ์ฃผ์ผ์ด ์ง€๋‚ฌ๋‹ค. ์ด์ œ ๋‚˜๋„ ์ •์‹ ์„ ์ฐจ๋ฆฌ๊ณ  ๋‚ด ๋ฏธ๋ž˜๋ฅผ ์œ„ํ•ด, ์นœ๊ตฌ์™€ ์•ฝ์†ํ•œ ๋Œ€๋กœ ์„ฑ๊ณต์ ์ธ 2022๋…„์„ ์œ„ํ•ด ๊ณต๋ถ€์— ์˜จ ์ •์‹ ์„ ๋งค์ง„ํ•ด์•ผ๊ฒ ๋‹ค. ์นœ๊ตฌ์•ผ ํ•˜๋Š˜์—์„œ๋Š” ๊ผญ ํ–‰๋ณตํ•˜๊ธธ. ์šฐ๋ฆฌ ์ด์ œ ์›ํ•˜๋Š” ๊ฒƒ์„ ํ•˜๊ณ  ์‚ด์ž. ๊ทธ๋Ÿฐ ์˜๋ฏธ๋กœ ์Šค์œ„ํ”„ํŠธ ๊ณต์‹๋ฌธ์„œ 3ํšŒ์ฐจ ์ •๋ฆฌ go

Swift ๊ณต์‹ Documents (3) strings and characters 

๋ฐ˜์‘ํ˜•
 

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

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

 

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

String(๋ฌธ์ž์—ด) ์€ "Hello, World" ์™€ ๊ฐ™์€ ์ผ๋ จ์˜ character(๋ฌธ์ž)๋“ค์ด ๋ฌถ์—ฌ์ง„ ๊ฒƒ์ด๋‹ค. Swift์—์„œ ๋ฌธ์ž์™€ ๋ฌธ์ž์—ด์€ ์ฝ”๋“œ์—์„œ ํ…์ŠคํŠธ ์œ ๋‹ˆ์ฝ”๋“œ ํ˜ธํ™˜๋ฐฉ๋ฒ•์œผ๋กœ ์ œ๊ณตํ•˜๋ฉฐ, ๋ฌธ์ž์—ด ๋ฐ ๋ฌธ์ž์˜ ์ƒ์„ฑ ๋ฌธ๋ฒ•์€ C์–ธ์–ด์™€ ๋น„์Šทํ•˜๋‹ค. ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์€ + ์—ฐ์‚ฐ์ž๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ๋ณ€์ˆ˜ ๋ฐ ์ƒ์ˆ˜์ฒ˜๋Ÿผ ์„ ์–ธํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 


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

๊ฐœ๋ฐœ์ž๋Š” ์ฝ”๋“œ ๋‚ด์— ๋ฏธ๋ฆฌ ์ •์˜ ๋œ String ๊ฐ’์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž์—ด์„ " " ์•ˆ์— ์จ์ฃผ๋ฉด ๋œ๋‹ค.

let someString = "Some string literal value"

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ฉด, someString์€ Swift๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ๋œ๋‹ค.

 

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

๋งŒ์•ฝ ๋ฌธ์ž์—ด์„ ์—ฌ๋Ÿฌ ์ค„์— ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด """ ๋ฅผ ํ™œ์šฉํ•ด๋ผ.

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.

"""

 

๋ฉ€ํ‹ฐ๋ผ์ธ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์—์„œ ์•ž์˜ ๊ณต๋ฐฑ์€ ์ฒซ์ค„์ด ์‹œ์ž‘ํ•  ๋•Œ์˜ ๊ณต๋ฐฑ๋งŒํผ์€ ๋ฌด์‹œ๋˜์ง€๋งŒ, ๊ทธ ์ดํ›„์˜ ์ค„์—์„œ๋Š” ์ฒซ์ค„ ๊ณต๋ฐฑ๋งŒํผ์€ ๋ฌด์‹œ๋˜๊ณ , ์•ž์— ๋„์›Œ์ง„ ๋‹ค๋ฅธ ๊ณต๋ฐฑ์€ ๋ฌด์‹œ๋˜์ง€ ์•Š๋Š”๋‹ค. ์•„๋ž˜์˜ ์‚ฌ์ง„์„ ๋ณด๋ฉด ๋ฌด์Šจ ๋ง์ธ์ง€ ์ดํ•ด๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

 

Special Characters in String Literals (๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ๋‚ด์˜ ํŠน์ˆ˜ ๋ฌธ์ž)

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

  • \0 : null character
  • \\ : back slash ๋ฐฑ์Šฌ๋ž˜์‰ฌ๋Š” ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • \t : ํƒญ ํ‚ค ํ•œ ๋ฒˆ ๋ˆ„๋ฅธ ๊ฒƒ
  • \n : ์ค„๋ฐ”๊ฟˆ
  • \r : carriage return์œผ๋กœ ์ปค์„œ์˜ ์œ„์น˜๋ฅผ ์ค„์˜ ๋งจ ์ฒ˜์Œ์œผ๋กœ ๋ณด๋‚ด ์คŒ
  • \" : ๋ฌธ์ž์—ด์—์„œ ํฐ ๋”ฐ์˜ดํ‘œ
  • \' : ๋ฌธ์ž์—ด์—์„œ ์ž‘์€ ๋”ฐ์˜ดํ‘œ
  • \u(n) : ๋ฌธ์ž์—ด์—์„œ ์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ ์Šค๊ณ  ์‹ถ์„ ๋•Œ (n์—๋Š” 1-8์ž๋ฆฌ์˜ 16์ง„์ˆ˜๋ฅผ ์จ์ฃผ๋ฉด ๋จ)
let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// "Imagination is more important than knowledge" - Einstein
let dollarSign = "\u{24}"        // $,  Unicode scalar U+0024
let blackHeart = "\u{2665}"      // ♥,  Unicode scalar U+2665
let sparklingHeart = "\u{1F496}" // ๐Ÿ’–, Unicode scalar U+1F496

๋งŒ์•ฝ ๋ฉ€ํ‹ฐ ๋ผ์ธ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ๋‚ด๋ถ€์—์„œ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๊ทธ๋ƒฅ ์จ๋„ ๋œ๋‹ค.(", "") ํ•˜์ง€๋งŒ ๋ฉ€ํ‹ฐ๋ผ์ธ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ (""")๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ๋งŒ์•ฝ ๋ฌธ์ž์—ด ๋‚ด๋ถ€์— """ ๋ฅผ ํฌํ•จํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฑ์Šฌ๋ž˜์‰ฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

let threeDoubleQuotationMarks = """
Escaping the first quotation mark \"""
Escaping all three quotation marks \"\"\"
"""

 

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

์•„๊นŒ์™€ ๊ฐ™์ด ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ๊ท€์ฐฎ๊ฑฐ๋‚˜ \n ๊ณผ ๊ฐ™์€ ๋ฌธ์ž๋ฅผ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด # ์•ˆ์— ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ทธ๋ƒฅ ๊ทธ ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทผ๋ฐ ๋งŒ์•ฝ #๋ฅผ ์“ฐ๊ณ  ๊ทธ ์•ˆ์— \n๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด #๊ฐœ์ˆ˜์™€ ๊ฐ™์€ ์ˆ˜์˜ #๋ฅผ \n์— ์จ์ฃผ๋ฉด ๋˜๋Š”๋ฐ ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด๋ณด์ž.

let threeMoreDoubleQuotationMarks = #"""
Here are three more double quotes: """
"""#

 


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

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

์ฆ‰, ๋นˆ ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š”๋ฐ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค.

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

๋˜ํ•œ ์ด๋ ‡๊ฒŒ ์ •์˜ํ•˜๋ฉด , String ๊ฐ’์€ isEmpty ๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, ์ด๊ฒƒ์„ if๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. (์•„๋ž˜)

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

String Mutability (๋ฌธ์ž์—ด ๋ณ€ํ˜•์„ฑ)

๊ฐœ๋ฐœ์ž๊ฐ€ String '๋ณ€์ˆ˜'๋กœ ์„ ์–ธํ•˜์˜€๋‹ค๋ฉด ๋ฌธ์ž์—ด์€ ๋ณ€ํ˜•ํ•  ์ˆ˜ ์ž‡๊ณ , ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜์˜€๋‹ค๋ฉด ๋ณ€ํ˜• ํ•  ์ˆ˜ ์—†๋‹ค.

var variableString = "Horse"
variableString += " and carriage"
// variableString is now "Horse and carriage"

let constantString = "Highlander"
constantString += " and another Highlander"
// this reports a compile-time error - a constant string cannot be modified

Strings Are Value Types

swift์˜ String ํƒ€์ž…์€ Value , ์ฆ‰ ๊ฐ’ ํƒ€์ž…์ด๋‹ค. ๊ฐœ๋ฐœ์ž๊ฐ€ ์ƒˆ๋กœ์šด string๊ฐ’์„ ๋งŒ๋“ค์–ด ๋‚ด๋ฉด ๊ทธ ๊ฐ’์ด ํ•จ์ˆ˜๋‚˜ ๋งค์„œ๋“œ์—์„œ ์‚ฌ์šฉ๋  ๋•Œ ๋ณต์‚ฌ๋˜์–ด ์‚ฌ์šฉ๋œ๋‹ค. ์ฆ‰ ๊ธฐ์กด์˜ ๋‹ค๋ฅธ ๊ฐ’๋“ค๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์•„์˜ˆ ์ƒˆ๋กœ์šด ๊ฐ’์ด ์ƒ์„ฑ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž. ๋ฌผ๋ก  ๋ณต์‚ฌ๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด์šฉ์€ ๊ฐ™๋‹ค.


Working with Characters

String์—์„œ charcacter๋Š” ํ•œ ๊ธ€์ž์”ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜ for-in ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ํ•œ ๊ธ€์ž์”ฉ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

for character in "Dog!๐Ÿถ" {
    print(character)
}
// D
// o
// g
// !
// ๐Ÿถ

๋งŒ์•ฝ ํ•œ char๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

let exclamationMark: Character = "!"
print(exclamationMark)
// !

์•„๋ž˜์™€ ๊ฐ™์ด String ๊ฐ’์€ character์˜ ๋ฐฐ์—ด๋กœ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

let catCharacters: [Character] = ["C", "a", "t", "!", "๐Ÿฑ"]
let catString = String(catCharacters)
print(catString)
// Prints "Cat!๐Ÿฑ"

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

string์€ ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์ž (+)๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. (concat)

let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome now equals "hello there"

๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์ž ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ += ์—ฐ์‚ฐ์ž๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

var instruction = "look over"
instruction += string2
// instruction now equals "look over there"

Character๊ฐ’์„ String์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ String ํƒ€์ž…๊ฐ„์˜ append() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ž. (Character๋Š” ํ•œ ๊ธ€์ž๋งŒ ์กด์žฌํ•˜๋Š” '๋ฌธ์ž'์ด๋ฏ€๋กœ, character๋ณ€์ˆ˜์—๋Š” appendํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์€ ๋‹น์—ฐํ•˜๋‹ค!)

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

๋งŒ์•ฝ ๋ฉ€ํ‹ฐ ๋ผ์ธ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ๋งˆ์ง€๋ง‰ ์ค„์—์„œ ๊ฐœํ–‰์ด ์ผ์–ด๋‚˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์ค˜์•ผ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ํ•ฉ์น  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

let badStart = """
one
two
"""
let end = """
three
"""
print(badStart + end)
// Prints two lines:
// one
// twothree

let goodStart = """
one
two

"""
print(goodStart + end)
// Prints three lines:
// one
// two
// three

String Interpolation (๋ฌธ์ž์—ด ๋ณด๊ฐ„)

๋ฌธ์ž์—ด ๋ณด๊ฐ„์€ String์— ์ƒ์ˆ˜, ๋ณ€์ˆ˜ , ๋ฆฌํ„ฐ๋Ÿด, ์—ฐ์‚ฐ ๋“ฑ์˜ ๊ฐ’์„ ๋„ฃ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๋ฌธ์ž์—ด ๋ณด๊ฐ„์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” \()๋ฅผ ๋ฌธ์ž์—ด์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ด๋ผ.

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

์ด๋Ÿฌํ•œ ๋ฌธ์ž์—ด ๋ณด๊ฐ„๋„ ์•ž์—์„œ ๋ฐฐ์šด ๊ฒƒ๊ณผ ๊ฐ™์ด #์„ ์“ฐ๋ฉด ๋ฌดํšจํ™” ๋œ๋‹ค. #๋ฅผ ๋ฌธ์ž ์•ž์— ๋ถ™์ด๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ธด ํ•˜๋‹ค.

print(#"Write an interpolated string in Swift using \(multiplier)."#)
// Prints "Write an interpolated string in Swift using \(multiplier)."

print(#"6 times 7 is \#(6 * 7)."#)
// Prints "6 times 7 is 42."

Unicode (์œ ๋‹ˆ์ฝ”๋“œ)

์œ ๋‹ˆ์ฝ”๋“œ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ธ์ฝ”๋”ฉ, ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ตญ์ œ ํ‘œ์ค€์ด๋‹ค. ์–ด๋– ํ•œ ๋ฌธ์ž์™€ ์–ธ์–ด๋„ ํ‘œ์ค€ํ™”๋œ ํ˜•์‹์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์™ธ๋ถ€ ์†Œ์Šค๋‚˜ ์›น์—์„œ๋„ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ๋‹ค. Swift์˜ String, Characterํƒ€์ž…๋„ ์œ ๋‹ˆ์ฝ”๋“œ ํ˜•์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

Unicode Scalar Values

swift์˜ native Stringํƒ€์ž…์€ ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ ๊ฐ’์€ ๊ณ ์œ ํ•œ 21๋น„ํŠธ ์ˆ˜์ด๋‹ค. ๋ชจ๋“  21๋น„ํŠธ ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ ๊ฐ’์ด ๋ฌธ์ž์— ํ• ๋‹น๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ  ํ–ฅํ›„ ์ถ”๊ฐ€๊ฐ€ ๋˜๊ฑฐ๋‚˜ UTF-16์ธ์ฝ”๋”ฉ์— ์‚ฌ์šฉ๋˜๋„๋ก ์˜ˆ์•ฝ๋˜์–ด ์žˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๋ฉด, U+0061dms ("a") ๋‚˜ํƒ€๋‚ด๋ฉฐ, U+1F425๋Š” ("๐Ÿฅ")๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. 

Extended Grapheme Clusters

Swift์˜ Characterํƒ€์ž…์€ ํ•˜๋‚˜์˜ graphemeํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ํ™•์žฅ๋œ Grapheme Cluster๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ ์‹œํ€€์Šค๋กœ ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (์•„๋ž˜ ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ ์œ ๋‹ˆ์ฝ”๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜•์„ฑ๋˜๊ณ  ์–ด๋–ป๊ฒŒ ๋ณ€ํ˜•๋˜๋Š”์ง€? ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…๋˜์–ด ์žˆ๋Š”๋ฐ ์ž์„ธํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ณ  ์ฝ”๋“œ๋งŒ ์ฒจ๋ถ€ํ•ด ๋†“์Œ)

let eAcute: Character = "\u{E9}"                         // é
let combinedEAcute: Character = "\u{65}\u{301}"          // e followed by ฬ
// eAcute is é, combinedEAcute is eฬ

ํ•œ๊ธ€์€ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„๋ ๊นŒ?
"ํ•œ"์ด๋ผ๋Š” ๊ธ€์ž๋กœ ์˜ˆ๋ฅผ ๋“ค๋ฉด "ํ•œ"์ด๋ผ๊ณ  ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ๊ณ  "ใ…Ž" + "ใ…" + "ใ„ด" ์œผ๋กœ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. (๊ณต์‹๋ฌธ์„œ์— ์ด๋ ‡๊ฒŒ ํ•œ๊ธ€์ด ์„ค๋ช…๋˜์–ด ์žˆ์œผ๋‹ˆ๊นŒ ๋ญ”๊ฐ€ ๋ฟŒ๋“ฏํ•˜๊ณ  ์‹ ๊ธฐํ•˜๋‹ค ํžˆํžˆํžˆ)

let precomposed: Character = "\u{D55C}"                  // ํ•œ
let decomposed: Character = "\u{1112}\u{1161}\u{11AB}"   // แ„’, แ…ก, แ†ซ
// precomposed is ํ•œ, decomposed is แ„’แ…กแ†ซ

์ฆ‰, Extended Grapheme Clusters๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹จ์ผ ์Šค์นผ๋ผ์— ๋‹ค๋ฅธ ์Šค์นผ๋ผ ๊ฐ’์„ ๋ฌถ์–ด์„œ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.


Counting Characters (๋ฌธ์ž์—ด ๊ธธ์ด count)

String์€ Count๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•ด๋‹น String์ด ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์˜ ์ˆ˜(string์˜ ๊ธธ์ด) ๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

let unusualMenagerie = "Koala ๐Ÿจ, Snail ๐ŸŒ, Penguin ๐Ÿง, Dromedary ๐Ÿช"
print("unusualMenagerie has \(unusualMenagerie.count) characters")
// Prints "unusualMenagerie has 40 characters"

์•„๊นŒ ๋ณธ Extended grapheme clusters์—์„œ é๋Š” ๋‘๊ฐœ์˜ ์Šค์นผ๋ผ๋กœ ์ด๋ฃจ์–ด ์กŒ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ด๋Ÿฐ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ String์˜ count ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์€ ์–ผ๋งˆ์ผ๊นŒ?

var word = "cafe"
print("the number of characters in \(word) is \(word.count)")
// Prints "the number of characters in cafe is 4"

word += "\u{301}"    // COMBINING ACUTE ACCENT, U+0301

print("the number of characters in \(word) is \(word.count)")
// Prints "the number of characters in cafeฬ is 4"

์œ ๋‹ˆ์ฝ”๋“œ๋กœ ์“ฐ์—ฌ์ง„ string๋„ ์•Œ๊ธฐ ์‰ฝ๊ฒŒ ์ถœ๋ ฅ๋œ ๋ฌธ์ž์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ๋ฌธ์ž๋“ค์ด ๋‹ค๋ฅธ ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, count ํ”„๋กœํผํ‹ฐ์— ์˜ํ•ด ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์ˆ˜๋Š” NSString์˜ length ํ”„๋กœํผํ‹ฐ์™€๋Š” ๋™์ผํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. NSString ์˜ lengthํ”„๋กœํผํ‹ฐ๋Š” UTF-16 ํ‘œํ˜„ ๋‚ด 16๋น„ํŠธ ์ฝ”๋“œ ๋‹จ์œ„ ์ˆ˜๋กœ ๋ฌธ์ž์˜ ์ˆ˜๋ฅผ ์„ผ๋‹ค.


 

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

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

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

stringํƒ€์ž…์€ indexํƒ€์ž…์ด๋‹ค. String.index ํ”„๋กœํผํ‹ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•ด๋‹น ์œ„์น˜์— ์žˆ๋Š” character์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ„์—์„œ ๋ฐฐ์› ๋“ฏ์ด, ๊ฐ๊ฐ์˜ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅธ ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— swift๋ฌธ์ž์—ด์€ ์ •์ˆ˜ ๊ฐ’์œผ๋กœ Index๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.

์šฐ์„  startIndex๋Š” string์˜ ์ฒซ character์— ์ ‘๊ทผํ•˜๋Š” ์œ„์น˜์ด๋ฉฐ, endIndex๋Š” ๋งˆ์ง€๋ง‰ character์— ์ ‘๊ทผํ•˜๋Š” ์œ„์น˜์ด๋‹ค. ๋งŒ์•ฝ ๋นˆ 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

๋งŒ์•ฝ String ๋ฒ”์œ„์—์„œ ๋ฒ—์–ด๋‚œ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

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

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

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

 

Inserting and Removing (๋ฌธ์ž์—ด ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ) : string.insert(), sring.remove()

String์˜ ํŠน์ • ์ธ๋ฑ์Šค์— 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!"

remove(์‚ญ์ œ)๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ๊ธ€์ž๋งŒ ์‚ญ์ œํ•  ๊ฒฝ์šฐ remove(:at:)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ณ , ๋ฌธ์ž์—ด์ด๋‚˜ ํŠน์ •ํ•œ ๋ฒ”์œ„๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” removeSubragne(_:) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there"
// hello there!์—์„œ !๊ฐ€ ์‚ญ์ œ๋จ (๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค before)

let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome now equals "hello"
// ๋ณดํ†ต ๋งจ ๋’ค์˜ ์ธ๋ฑ์Šค๊ฐ€ -1 ์ด๋‹ˆ๊นŒ, -6๋ถ€ํ„ฐ (๊ณต๋ฐฑ) endindex before๊นŒ์ง€ ์‚ญ์ œํ•˜๋ฉด๋œ๋‹ค. (๋๊นŒ์ง€)
// swift์—์„œ๋Š” ๋งˆ์ง€๋ง‰์„ endindex before ๋กœ ์ธ์‹ํ•˜๋‚˜๋ณด๋‹ค.

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

String์—์„œ prefix(_:)์™€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋กœ substring์„ ๊ฐ€์ง€๊ณ  ์˜ค๋ฉด ๊ฒฐ๊ณผ๋Š” Substring ์ธ์Šคํ„ด์Šค๊ฐ€ ๋œ๋‹ค. swift์—์„œ substring๊ณผ ๊ฐ™์€ string์€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž๋Š” ๊ทธ๋ƒฅ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ substring์€ string๊ณผ ๋‹ค๋ฅด๊ฒŒ 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๊ณผ substring์˜ ์ฐจ์ด๋ฅผ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋‹ค. substring์€ ๊ธฐ์กด์˜ string์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ substring์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ๊ธฐ์กด์˜ string์ด๋‚˜ substring์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ณต์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‚ญ๋น„๋ฅผ ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ substring์€ ๊ธฐ์กด์˜ string์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด๋Ÿฌํ•œ ๋ฐฉ์‹ ๋•Œ๋ฌธ์— substring์„ ์‚ฌ์šฉํ•˜๋Š” ๋™์•ˆ์—๋Š” ๊ธฐ์กด์˜ string์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋ณด๊ด€ํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰, substring์„ ์˜ค๋ž˜ ์ง€์†ํ•˜๊ธฐ์—๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค.

์ถœ์ฒ˜: Swift ๊ณต์‹๋ฌธ์„œ 3๋‹จ์›


Comparing Strings (๋ฌธ์ž์—ด ๋น„๊ต)

swift๋Š” ํ…์ŠคํŠธ ๊ฐ’์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค : ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž ๋™์ผ์„ฑ ํŒ๋‹จ, ์ ‘๋‘์‚ฌ ๋™์ผ์„ฑ ํŒ๋‹จ, ์ ‘๋ฏธ์‚ฌ ๋™์ผ์„ฑ ํŒ๋‹จ.

String and Character Equality (๋ฌธ์ž ๋ฐ ๋ฌธ์ž์—ด ๋™์ผ์„ฑ) : ==, !==

๋ฌธ์ž์—ด ๋ฐ ๋ฌธ์ž ๋™์ผ์„ฑ์€ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ๋™์ผํ•˜๊ฒŒ  ๋™์ผ ์—ฐ์‚ฐ์ž (==) ๊ฐ™์ง€์•Š์Œ ์—ฐ์‚ฐ์ž (!=)๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

let quotation = "We're a lot alike, you and I."
let sameQuotation = "We're a lot alike, you and I."
if quotation == sameQuotation {
	print("These two strings are considered equal")
}
// Prints "These two strings are considered equal"

 

extended grapheme clusters๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ ๋‘ ๋ฌธ์ž์—ด๋„ ๋™์ผํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. ์•„๊นŒ ๋ณธé์™€ ๊ฐ™์ด ๋‘ ๊ฐœ์˜ ์Šค์นผ๋ผ๋กœ ์ด๋ฃจ์–ด ์ง„ ๋ฌธ์ž์ธ ๊ฒฝ์šฐ์—๋„ e์™€ ๋™์ผํ•œ ๋ฌธ์ž๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

// "Voulez-vous un café?" using LATIN SMALL LETTER E WITH ACUTE
let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"

// "Voulez-vous un cafeฬ?" using LATIN SMALL LETTER E and COMBINING ACUTE ACCENT
let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?"

if eAcuteQuestion == combinedEAcuteQuestion {
    print("These two strings are considered equal")
}
// Prints "These two strings are considered equal"

 

๋ฐ˜๋Œ€๋กœ ์˜์–ด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ผํ‹ด์–ด ๋Œ€๋ฌธ์ž A (U+0041, or "A") ๋Š” ๋Ÿฌ์‹œ์•„์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค๋ฆด๋ฌธ์ž A (U+0410, or "ะ")์™€๋Š” ๊ฐ™์ง€ ์•Š๋‹ค.

let latinCapitalLetterA: Character = "\u{41}"
let cyrillicCapitalLetterA: Character = "\u{0410}"

if latinCapitalLetterA != cyrillicCapitalLetterA {
    print("These two characters aren't equivalent.")
}
// Prints "These two characters aren't equivalent."

 

Prefix and Suffix Equality (์ ‘๋‘์‚ฌ ๋ฐ ์ ‘๋ฏธ์‚ฌ ๋™์ผ์„ฑ) : hasPrefix(), hasSuffix()

๋ฌธ์ž์—ด์— ํŠน์ • ๋ฌธ์ž์—ด ์ ‘๋‘์‚ฌ ๋˜๋Š” ์ ‘๋ฏธ์‚ฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ํ•˜๋ ค๋ฉด ๋ฌธ์ž์—ด์˜ 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"
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"

Unicode Representations of Strings

์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์ด ํ…์ŠคํŠธ ํŒŒ์ผ์ด๋‚˜ ๋‹ค๋ฅธ ์ €์žฅ์†Œ์— ์ €์žฅ๋  ๋•Œ ๋ฌธ์ž์—ด์˜ ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ ๊ฐ’์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ Unicode-defined encoding forms์— ์˜ํ•ด ์ธ์ฝ”๋”ฉ ๋œ๋‹ค. ์—ฌ๊ธฐ์—” UTF-8(8๋น„ํŠธ๋กœ ์ธ์ฝ”๋”ฉ), UTF-16(16๋น„ํŠธ๋กœ ์ธ์ฝ”๋”ฉ), UTF-32(32๋น„ํŠธ๋กœ ์ธ์ฝ”๋”ฉ)๊ฐ€ ์žˆ๋‹ค.

Swift๋Š” ๋ฌธ์ž์—ด์˜ ์œ ๋‹ˆ์ฝ”๋“œ ํ‘œํ˜„์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์„ธ ๊ฐ€์ง€์˜ ์œ ๋‹ˆ์ฝ”๋“œ ํ‘œํ˜„ ๋ฐฉ์‹์œผ๋กœ String ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.(UTF-8, UTF-16, UTF-32) ๊ทธ๋Ÿผ ์„ธ ๊ฐ€์ง€ ์œ ๋‹ˆ์ฝ”๋“œ ํ‘œํ˜„ ๋ฐฉ์‹์˜ ์ฐจ์ด๋ฅผ ์‚ดํŽด๋ณด์ž.

let dogString = "Dogโ€ผ๐Ÿถ"

 

UTF-8 Representation (8๋น„ํŠธ)

์ด๋Ÿฌํ•œ ์ฝ”๋“œ์˜ ์„ธ ๊ฐ€์ง€ ์œ ๋‹ˆ์ฝ”๋“œ ํ‘œํ˜„ ๋ฐฉ์‹์„ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

for codeUnit in dogString.utf8 {
    print("\(codeUnit) ", terminator: "")
}
print("")
// Prints "68 111 103 226 128 188 240 159 144 182 "

์„ธ ์ž๋ฆฌ ์‹ญ์ง„์ˆ˜๋กœ ๋ฌธ์ž๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒŒ ๋ฐ”๋กœ UTF-8 ํ‘œํ˜„์ด๋‹ค.

 

UTF-16 Representation (16๋น„ํŠธ) : string.UTF16View()

๊ฐœ๋ฐœ์ž๋Š” string.UTF16View ๋กœ UTF-16 ํ‘œํ˜„๋ฐฉ์‹์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ๋ถ€ํ˜ธ๊ฐ€ ์—†๋Š” 16๋น„ํŠธ๋กœ ํ‘œํ˜„ํ•˜๊ฒŒ ๋œ๋‹ค.

for codeUnit in dogString.utf16 {
    print("\(codeUnit) ", terminator: "")
}
print("")
// Prints "68 111 103 8252 55357 56374 "

 

Unicode Scalar Representation

์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ 10์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค ์œ ๋‹ˆ์ฝ”๋“œ๊ฐ€ 16์ง„์ˆ˜ ์ด๋‹ˆ ๊ทธ๋ƒฅ 16์ง„์ˆ˜๋ฅผ 10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

for scalar in dogString.unicodeScalars {
    print("\(scalar.value) ", terminator: "")
}
print("")
// Prints "68 111 103 8252 128054 "

์ฒ˜์Œ ์„ธ ๊ฐœ์˜ ๊ฐ’์€ D, o, g ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋„ค๋ฒˆ์งธ codeUnit ๊ฐ’ (8252)๋Š” 16์ง„์ˆ˜ ๊ฐ’ 203C์™€ ๊ฐ™์€ ์‹ญ์ง„๋ฒ•์ด๋ฉฐ, ์ด๋Š” U+203C๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๊ฐ ๊ฐ’๋“ค์„ unicodeScalar ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ณด๊ฐ„๊ณผ ๊ฐ™์ด ์ƒˆ ๋ฌธ์ž์—ด ๊ฐ’์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค. (์•„๋ž˜)

for scalar in dogString.unicodeScalars {
    print("\(scalar) ")
}
// D
// o
// g
// โ€ผ
// ๐Ÿถ

 

๋งˆ๋ฌด๋ฆฌํ•˜๋ฉฐ

๋.. ๊ธธ๋‹ค.. 1์›” ์ด๋‚ด๋กœ๋Š” ๊ผญ ์ด ๋ชจ๋“  ๋ฌธ๋ฒ• ์‚ฌํ•ญ์„ ์ตํžˆ๊ณ  ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ swift๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋…ธ๋ ฅํ•˜์ž!

๋ฐ˜์‘ํ˜•