Bibi's DevLog ๐Ÿค“๐ŸŽ

[Swift] String.Index (๊ทธ๋ฆฌ๊ณ  Substring) - ๋ฌธ์ž์—ด์˜ ํŠน์ • ๋ฌธ์ž ๊ตฌํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ“ฑ๐ŸŽ iOS/๐Ÿ•Š Swift

[Swift] String.Index (๊ทธ๋ฆฌ๊ณ  Substring) - ๋ฌธ์ž์—ด์˜ ํŠน์ • ๋ฌธ์ž ๊ตฌํ•˜๊ธฐ

๋น„๋น„ bibi 2022. 7. 28. 12:29

[Swift] String.Index (๊ทธ๋ฆฌ๊ณ  Substring) - ๋ฌธ์ž์—ด์˜ ํŠน์ • ๋ฌธ์ž ๊ตฌํ•˜๊ธฐ

์• ํ”Œ ๊ณต์‹ ๋ฌธ์„œ

Swift Standard Library > String > String.Index

https://developer.apple.com/documentation/swift/string/index/

Swift Standard Library > Substring

https://developer.apple.com/documentation/swift/substring

์„ค๋ช…๊ณผ ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ•œ ํ›Œ๋ฅญํ•œ ๊ธ€

http://seorenn.blogspot.com/2018/05/swift-string-index.html

String.Index

๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž๋‚˜ ์ฝ”๋“œ ์œ ๋‹›์˜ ์œ„์น˜.

: ์Šค์œ„ํ”„ํŠธ์—์„œ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ํ•œ ํƒ€์ž…์ด๋‹ค.

โ—๏ธ ์Šค์œ„ํ”„ํŠธ์˜ String์—์„œ๋Š” ์ธ๋ฑ์Šค๋กœ ์ •์ˆ˜(Int)๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ๊ทธ ๋Œ€์‹  String.Index๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

var hello = "hello" ๋ผ๊ณ  ํ•˜์ž.

๋ฌธ์ž์—ด์˜ ๋งจ ์ฒซ ๊ธ€์ž ๊ตฌํ•˜๊ธฐ

  • prefix(n) : ์•ž์—์„œ๋ถ€ํ„ฐ n๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ
    • let he = hello.prefix(2)
  • startIndex ํ™œ์šฉํ•˜๊ธฐ
    • let first = hello[hello.startIndex]
  • String.Index(encodedOffset: 0) : 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ธ๋ฑ์Šค ์ƒ์„ฑ

๋ฌธ์ž์—ด์˜ ๋‘ ๋ฒˆ์งธ ๊ธ€์ž ๊ตฌํ•˜๊ธฐ

๋‘ ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•œ ๋’ค, ๊ทธ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•ด ๋‘ ๋ฒˆ์จฐ ๊ธ€์ž๋ฅผ ์ฐพ๋Š”๋‹ค.

  • let secondIndex = hello.index(after: hello.startIndex)
  • let second = hello[secondIndex]

๋ฌธ์ž์—ด์˜ n๋ฒˆ์งธ ๊ธ€์ž ๊ตฌํ•˜๊ธฐ

func String.index(Substring.Index, offsetBy: Int) -> Substring.Index ๋ฅผ ํ™œ์šฉํ•ด ๊ตฌํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์„ธ ๋ฒˆ์งธ ๊ธ€์ž ๊ตฌํ•˜๊ธฐ์˜ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด offsetBy๋ฅผ 2 ๋กœ ์„ค์ •ํ•ด ๊ตฌํ•œ๋‹ค

(์ธ๋ฑ์Šค ๊ฐ’์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค)

  • let thirdIndex = hello.index(hello.startIndex, offsetBy: 2)

๋ฌธ์ž์—ด์˜ n๋ฒˆ์งธ ์ดํ›„ ๋ชจ๋“  ๊ธ€์ž ๊ตฌํ•˜๊ธฐ

์ธ๋ฑ์Šค์˜ ๋ฒ”์œ„ ํ‘œํ˜„ ํ™œ์šฉ.

let s = "12:00:00AM"
let timeIndex = s.index(s.startIndex, offsetBy: 7)
let timeStr = String(s[...timeIndex]) // 12:00:00
let ampmIndex = s.index(s.endIndex, offsetBy: -2)
let ampmStr = String(s[ampmIndex...]) // AM

// ๋˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค
let timeStr2 = s.prefix(8) // 12:00:00
let ampmStr2 = s.suffix(2) // AM

๋ฌธ์ž์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰ ๊ธ€์ž ๊ตฌํ•˜๊ธฐ

  • subfix(n) : ๋’ค์—์„œ๋ถ€ํ„ฐ n๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ
    • let lo = hello.suffix(2)
  • endIndex ์™€ index(before:) ํ™œ์šฉํ•˜๊ธฐ
    • endIndex๋Š” ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ธ€์ž๊ฐ€ ์•„๋‹Œ ๋งจ ๋์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    • โŒ hello[hello.endIndex] -> ์—๋Ÿฌ.
    • โญ•๏ธ let endIndex = hello.index(before: hello.endIndex)์„ ๊ตฌํ•œ ๋’ค, let last = hello[endIndex] ๋กœ ๊ตฌํ•œ๋‹ค.
  • let endIndex = hello.index(hello.endIndex, offsetBy: -1)
    • ์ธ๋ฑ์Šค๋กœ๋ถ€ํ„ฐ ์™ผ์ชฝ์œผ๋กœ ์›€์ง์ผ ๋•Œ๋Š” offsetBy ๊ฐ’์œผ๋กœ ์Œ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ค€๋‹ค.

๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค ๊ตฌํ•˜๊ธฐ

func firstIndex(of: Character) -> String.Index? ๋˜๋Š”

func lastIndex(of: Character) -> String.Index? ๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด o๋ผ๋Š” ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

  • let oIndex = hello.index(of: "o")

โ—๏ธString.Index ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ์œ„์˜ ๋ฉ”์„œ๋“œ๋“ค์€ ์‚ฌ์‹ค Substring์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด๋‹ค!

๋ฌธ์ž์—ด ์ž˜๋ผ๋‚ด๊ธฐ

์Šค์œ„ํ”„ํŠธ์˜ substring = ๋ถ€๋ถ„๋ฌธ์ž์—ด ์„ ์˜๋ฏธํ•œ๋‹ค.

๋ถ€๋ถ„๋ฌธ์ž์—ด์€ String.Index๋ฅผ ํ™œ์šฉํ•ด ๋ฒ”์œ„(range)๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ , ๊ทธ ๋ฒ”์œ„๋ฅผ ์ด์šฉํ•ด ๊ตฌํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด var hello = "hello" ์—์„œ ๋งจ ์•ž, ๋งจ ๋’ค ๊ธ€์ž๋ฅผ ์ œ์™ธํ•œ ell๋งŒ ๊ตฌํ•ด ๋ณด์ž.

let rangeStartIndex = hello.index(after: hello.startIndex)
let rangeEndIndex = hello.index(hello.index, offsetBy: -2)
let ellSubstring = hello[rangeStartIndex...rangeEndIndex] // Substring ํƒ€์ž…์˜ "ell" ์ƒ์„ฑ

// ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์œ„์˜ ์ฝ”๋“œ์™€ ๊ฐ™์€ ์—ญํ• ์ด๋‹ค.

let rangeStartIndex = hello.index(after: hello.startIndex)
let rangeEndIndex = hello.index(before: hello.endIndex)
let ellSubstring = hello[rangeStartIndex..<rangeEndIndex] // ..< ์„ ์‚ฌ์šฉํ•จ์— ์ฃผ์˜

์ธ๋ฑ์Šค์˜ ๋ฒ”์œ„๋ฅผ ์ด์šฉํ•ด ์–ป์€ ๋ถ€๋ถ„๋ฌธ์ž์—ด์€ String์ด ์•„๋‹Œ Substring์ด๋ผ๋Š” ๋ณ„๋„์˜ ํƒ€์ž…์ด๋ฏ€๋กœ, String์œผ๋กœ ์บ์ŠคํŒ…ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

  • let ellString = String(ellSubstring)