๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

์ดˆ๋ฐ˜์€  ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋Š” ์ง€์‹ ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ํŒจ์Šคํ•˜๊ณ   ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ์ง€์‹  ์œ„์ฃผ๋กœ ์ •๋ฆฌํ•ด ๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

 

 

// ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์— ํŠน์ˆ˜ ๋ฌธ์ž
let sparklingHeart = "\\u{1F496}" // ๐Ÿ’–, Unicode scalar U+1F496

// ๋ฌธ์ž์—ด ๋ณ€๊ฒฝ
var variableString = "Horse"
variableString += " and carriage" // "Horse and carriage"
  • ๋ฌธ์ž์—ด์€ ๊ฐ’(value) ํƒ€์ž…์ด๋‹ค.
    • ์›๋ณธ์ด ์•„๋‹Œ ์ƒˆ๋กœ์šด ๋ณต์‚ฌ๋ณธ์ด ์ „๋‹ฌ๋œ๋‹ค.

 

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

  • ์ธ์ฝ”๋”ฉ, ํ‘œ๊ธฐ, ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์“ฐ๊ธฐ ์‹œ์Šคํ…œ์—์„œ์˜ ํ…์ŠคํŠธ ํ”„๋กœ์„ธ์‹ฑ์„ ์œ„ํ•œ ๊ตญ์ œ ํ‘œ์ค€์ด๋‹ค.
  • Swift์˜ String ๊ณผ Character ํƒ€์ž…์€ ์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ์ง€์›ํ•œ๋‹ค.

Swift์˜ ๊ธฐ๋ณธ String ํƒ€์ž…์€ ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ ๊ฐ’ (Unicode scalar values) ์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ

var word = "cafe"
word += "\\u{301}" // café
  • Character ๊ฐ’์„ ์œ„ํ•œ ํ™•์žฅ๋œ ๋ฌธ์ž์†Œ ํด๋Ÿฌ์Šคํ„ฐ์‚ฌ์šฉ์€ ๋ฌธ์ž์—ด ๋์— ์ถ”๊ฐ€๊ฐ€ ์•„๋‹ˆ๋ผ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์—ด๋งŒ ๋ณ€๊ฒฝ๋œ๋‹ค.

 

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

let greeting = "Guten Tag!"

// 'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.
// String.Index๋ฅผ ์จ์ค˜์•ผ ํ•จ
print(greeting[7]) 

let index = greeting.index(greeting.startIndex, offsetBy: 7)
print(greeting[index]) // a

 

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

var welcome = "hello"
welcome.insert("!", at: welcome.endIndex) 
// hello!

welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex)) 
// hello there!
  • ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค๋ฉด insert(_:at:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…ํ•˜๋ ค๋ฉด insert(contentsOf:at:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

var welcome = "hello there!"
welcome.remove(at: welcome.index(before: welcome.endIndex))
// hello there

let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// hello
  • ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด remove(at:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฒ”์œ„์˜ ๋ฌธ์ž์—ด์„ ์‚ญ์ œํ•˜๋ ค๋ฉด removeSubrange(_:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

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

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

let newString = String(beginning)
  • ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด
    • Substrings์ด ์›๋ž˜ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ผ๋ถ€ ๋˜๋Š” ๋‹ค๋ฅธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์ผ๋ถ€๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค..!
    • ์ฆ‰ ์›๋ž˜ ๋ฌธ์ž์—ด์˜ ์ €์žฅ์†Œ๋ฅผ ์žฌ์‚ฌ์šฉํ•œ๋‹ค.
  • beginning ์€ greeting ์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— greeting ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์žฌ์‚ฌ์šฉ
  • ewString ์€ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์ฆ‰ ์ด๊ฒƒ์€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์—์„œ ์ƒ์„ฑ๋  ๋•Œ ์ž์‹ ๋งŒ์˜ ์ €์žฅ์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค.

 

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

// 1. ๋น„๊ต ์—ฐ์‚ฐ์ž == ๋กœ ๋ฌธ์ž์—ด ๋น„๊ต
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")
}

// 2. hasPrefix(_:) ๋ฉ”์†Œ๋“œ๋กœ ์ ‘๋‘์‚ฌ ๋น„๊ต
let a = "apple pie"
print(a.hasPrefix("ap"))

// 3. hasSuffix(_:) ๋ฉ”์†Œ๋“œ๋กœ ์ ‘๋ฏธ์‚ฌ ๋น„๊ต
print(a.hasSuffix("pie"))

 

๋ฌธ์ž์—ด์˜ ์œ ๋‹ˆ์ฝ”๋“œ ํ‘œํ˜„ (Unicode Representations of Strings)

  • ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์ด ํ…์ŠคํŠธ ํŒŒ์ผ ๋˜๋Š” ๋‹ค๋ฅธ ์ €์žฅ์†Œ์— ์“ฐ์—ฌ์งˆ ๋•Œ ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ๋Š” ์ •์˜๋œ ์œ ๋‹ˆ์ฝ”๋“œ ์ธ์ฝ”๋”ฉ ํ˜•์‹ ์ค‘์— ํ•˜๋‚˜๋กœ ์ธ์ฝ”๋”ฉ ๋œ๋‹ค.
    • UTF-8 ์ธ์ฝ”๋”ฉ ํ˜•์‹ (8-bit ์ฝ”๋“œ ์œ ๋‹›์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ธ์ฝ”๋”ฉ)
    • UTF-16 ์ธ์ฝ”๋”ฉ ํ˜•์‹ (16-bit ์ฝ”๋“œ ์œ ๋‹›์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ธ์ฝ”๋”ฉ)
    • UTF-32 ์ธ์ฝ”๋”ฉ ํ˜•์‹ (32-bit ์ฝ”๋“œ ์œ ๋‹›์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ธ์ฝ”๋”ฉ)
profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...