Focus On Develop ๐ŸคŸ๐ŸคŸ

[Swift] String.components ๋ณธ๋ฌธ

iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!

[Swift] String.components

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2020. 12. 18. 14:40

์ƒ์„ธ ๋‚ด์šฉ : components | Apple Document

 

 

์ด๊ฑธ ์™œ ์ด์ œ ์•Œ์•˜์ง€..?? ใ…‹ใ…‹ ๊ทธ๋™์•ˆ ๊ณต๋ถ€ ๋„ˆ๋ฌด ์•ˆํ•œ๊ฑฐ ๋‹ค ํ‹ฐ๋‚˜ ๋ฒ„๋ ธ์ฅฌ ~~~ ๐Ÿ˜‚ ๐Ÿ˜‚

๋ฌธ์ž์—ด์„ ํŠน์ • String ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ NSArray ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ ๋ผ๊ณ  ํ•œ๋‹ค. 

์—ญ์‹œ ์ดํ•ดํ•˜๋Š”๋ฐ๋Š” ์˜ˆ์‹œ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•˜๋Š”๊ฒŒ ์ œ์ผ ๋น ๋ฅด๊ฒ ์ฃ ~~~!?

let string = "hello? my name@ is kt!!"

print(string.components(separatedBy: "na")) // ["hello? my ", "me@ is kt!!"]
print(string.components(separatedBy: "!")) // ["hello? my name@ is kt", "", ""]
print(string.components(separatedBy: "!").count) // 3
print(string.components(separatedBy: "?")) // ["hello", " my name@ is kt!!"]

์ด๊ฑธ ๋ณด๋ฉด separator๋ฅผ ๊ธฐ์ค€์œผ๋กœ separator๋Š” ๋‚ ๋ ค๋ฒ„๋ฆฌ๊ณ , ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋กœ ๋‚˜๋ˆ ์ค€ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ˆˆ์—ฌ๊ฒจ ๋ณผ๋งŒํ•œ ์ ์€ "hello? my name@ is kt!!" ์—ฌ๊ธฐ์„œ separator๋ฅผ "!" ๋กœ ์ง€์ •ํ–ˆ์„๋•Œ, ํŠน๋ณ„ํžˆ ๋ถ„๋ฆฌํ•ด์ค„ ๋ฌธ์ž๊ฐ€ ์—†๋”๋ผ๋„ ๋ฐฐ์—ด์˜ ์š”์†Œ๋กœ "" ๋นˆ string๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. ์Œ..? ์™œ ๋ˆˆ์—ฌ๊ฒจ ๋ดค๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ์–ธ์  ๊ฐ€ ๋– ์˜ฌ๋ฆด ๊ฒƒ ๊ฐ™๋‹ค. ใ…‹ใ…‹ ๐Ÿ˜‚

 

components๋ฅผ ์ ์šฉํ•œ๊ฒƒ์—์„œ .count๋ฅผ ํ•ด๋ณด๋ฉด ๋‚˜๋ˆ ์ง„ ์š”์†Œ์˜ ๊ฐฏ์ˆ˜ (๋ฐฐ์—ด์˜ ์นด์šดํŠธ์™€ ๋™์ผํ•˜์ฃ ~) ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ.. ์ด๋ง์€ ์ฆ‰์Šจ ์–ด๋–ค ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋‹จ์–ด, ํŠน์ • ๋ฌธ์žํŒจํ„ด ๋“ฑ์ด ์ฃผ์–ด์กŒ์„๋•Œ ์ด๊ฒŒ ๋ช‡๊ฐœ๋‚˜ ์žˆ๋‚˜์š”? ๋ผ๊ณ  ํ–ˆ์„๋•Œ, components๋กœ ๋ถ„๋ฆฌํ•œ ํ›„์— -1์„ ํ•ด์ฃผ๋ฉด ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒ ๋‹ค. (๋‚˜์ค‘์„ ์œ„ํ•œ ์ฐธ๊ณ  ใ…‹ใ…‹)

 

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ์ €๋ ‡๊ฒŒ ๋‚˜๋ˆ ์ง„ ๋ฐฐ์—ด์„ ๋‹ค์‹œ ๋ฌธ์ž๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด?? (Array, Set, Dictionary ๋“ฑ Sequence์— ๋ชจ๋‘ ์ ์šฉ)

let newString = string.components(separatedBy: "n").joined()
let newStringSeparator = string.components(separatedBy: "n").joined(separator: "^")
print(newString) // hello? my ame@ is kt!!
print(newStringSeparator) // hello? my ^ame@ is kt!!

์ด๋ ‡๊ฒŒ joined()๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๋ฌธ์ž์—ด๋กœ ํ•ฉ์น ๋•Œ ๋ญ”๊ฐ€๋ฅผ ๋„ฃ๊ณ ์‹ถ์œผ๋ฉด..!? joined(separator: ) ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹น~~

 

๊ทธ๋ฆฌ๊ณ  ์ด๊ฑด ๊ฐ‘์ž๊ธฐ ๋ฌธ๋“ ๊ถ๊ธˆํ•ด์„œ..? ๋งŒ์•ฝ ๋ฌธ์ž์—ด์˜ ์•ž, ๋’ค ๊ณต๋ฐฑ์„ ์ง€์šฐ๊ณ  ์‹ถ์œผ๋ฉด ์–ด์ฉŒ์ฃ !?!?!?

๋‹ค๋ฅธ ์–ธ์–ด๋“ค์ฒ˜๋Ÿผ .trim() ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋‚˜์š”!?!?!? ๋„ค!!!!! trimmingCharacters ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค!!!

let whiteSpace = " hello my name   "
print(whiteSpace.trimmingCharacters(in: [" "])) // "hello my name"
print(whiteSpace.trimmingCharacters(in: .whitespacesAndNewlines)) // "hello my name"

let trimCharacter = "#hello my name@@"
print(trimCharacter.trimmingCharacters(in: ["#"])) // "hello my name@@"
print(trimCharacter.trimmingCharacters(in: ["#", "@"])) // "hello my name"

๊ณต๋ฐฑ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์›ํ•˜๋Š” Character Set์„ ๋“ฑ๋กํ•ด์„œ trimming ํ•˜๋Š”๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค! ๐Ÿ‘๐Ÿ‘

ํŠนํžˆ defaults๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•œ Property์—๋Š” whitespacesAndNewlines, decimalDigits, lowercaseLetters ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•˜๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•ด์•ผ๊ฒ ๋‹ค!

 

Comments