Deep_Dev
article thumbnail

๐Ÿ“š ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ

split

components

 

 

 

 

Split

 

split๋ฉ”์„œ๋“œ๋Š” ๋‹ค์–‘ํ•œ parameter๋ฅผ ์ง€์›ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ์—ญํ• ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

- separater : ์ชผ๊ฐœ๋ ค๋Š” ๋ฌธ์ž ๋‹จ์œ„

- maxSplits : ์ง€์ •ํ•œ ๋ฌธ์ž ๋‹จ์œ„๋กœ ์–ผ๋งˆ๋‚˜ ์ชผ๊ฐค์ง€

- omittingEmptySubsequences : Bool๊ฐ’์œผ๋กœ ๊ฒฐ๊ณผ๊ฐ’์—์„œ ๋นˆ ์‹œํ€€์Šค์˜ ํฌํ•จ ์œ ๋ฌด๋ฅผ ์„ค์ •

 

์ฐธ๊ณ  : https://zeddios.tistory.com/232

 

split์€ ๋ง ๊ทธ๋Œ€๋กœ ๋ฌธ์ž์—ด์„ ํŠน์ • ๋ฌธ์ž ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐค ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜ํ™˜ํ˜•์€ Stringํƒ€์ž…์„ [Substring]์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค. ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

๋˜ํ•œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค. ๋•Œ๋ฌธ์— ๋ฐ”๋กœ import Foundation์„ ํ•  ํ•„์š”์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

let S = "Hello I'm JIho, Thank You!"

var result = S.split(separater: " ")
print(result)

result = S.split{ $0 == " "}
print(result)

 

 

 

๐Ÿ’ก ๋ฌธ์ž์—ด์—์„œ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋ฉด์„œ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž๋“ค์„ ๋ฐ›์œผ๋ ค๊ณ  ํ•  ๋•Œ ! 

 

split์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ณต๋ฐฑ๋ฌธ์ž๊ฐ€ ์—ฐ์†์œผ๋กœ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜ํƒ€๋‚˜๋”๋ผ๋„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค. ์ฆ‰, ์—ฐ์†๋œ ๊ณต๋ฐฑ์ด 3๊ฐœ๋“  5๊ฐœ๋“  ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์—ฐ์†๋œ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ์ทจ๊ธ‰ํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•œ๋‹ค.

var string = "   I Love You Thank    You"
print(string.split(separator: " ").map{String($0)}) // ["I", "Love", "You", "Thank", "You"]

 

 

 

 

 

Components

 

components๋„ split๊ณผ ์œ ์‚ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค. 

components(separatedBy:)๋˜ํ•œ split๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ธ์ž๊ฐ’์œผ๋กœ ๋ฌธ์ž๋ฅผ ๋ฐ›์•„ ๋ฌธ์ž ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐœ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

- separatedBy : ๋ถ„๋ฆฌํ•˜๋Š” ๊ธฐ์ค€ ๋ฌธ์ž

 

 

 

* splitr๊ณผ์˜ ์ฐจ์ด์ 

components๋Š” ๋ฐ˜ํ™˜ํ˜•์ด [String]์ด๋‹ค. ๋˜ํ•œ Components๋Š” Foundation ํ”„๋ ˆ์ž„์›Œํฌ์— ์†ํ•ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ Foundation ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ import ํ•ด์•ผํ•œ๋‹ค.

import Foundation 

let S = "Hello I'm JIho, Thank You!"

var result = S.components(separatedBy: " ")
print(result)

 

์œ„์™€ ๊ฐ™์ด import Foundation์„ ํ•ด์ฃผ์–ด์•ผ ๋ฌธ์ž์—ด ํƒ€์ž… ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ components ๋ฉ”์„œ๋“œ๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

์ดํ›„ ๊ฒฐ๊ณผ๊ฐ’์€ split๊ณผ ๊ฐ™์ด ๋ฐฐ์—ดํƒ€์ž…์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ด ๋‘˜์˜ ๊ฒฐ๊ณผ๊ฐ’์ด ๋™์ผํ•ด ๋ณด์ผ์ง€ ๋ชจ๋ฅด์ง€๋งŒ ๊ทธ ๊ณผ์ •์—์„œ๋Š” split, components ๊ฐ๊ฐ ๋™์ž‘ ๋ฐฉ์‹์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. 

 

 

 

 

split๊ณผ components ์ฐจ์ด

  • split : import Foundation ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • components : import Foundation ํ•„์š”ํ•˜๋‹ค.

 

  • split : [Substring] ๋ฐ˜ํ™˜
  • components : [String] ๋ฐ˜ํ™˜

 

  • split : separator์™ธ์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋งŽ๋‹ค.
  • components : separatedBy ์ธ์ž๊ฐ’ ๋”ฑ ํ•˜๋‚˜์ด๋‹ค.