๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

[Swift] ๋ฌธ์ž์—ด ๋‚ด๊ฐ€ ์›ํ•˜๋Š”๋งŒํผ ์ž๋ฅด๊ธฐ

by Fomagran ๐Ÿ’ป 2020. 1. 19.
728x90
๋ฐ˜์‘ํ˜•

๋‚˜๋งŒ์˜ ๋‹จ์–ด์žฅ ์–ดํ”Œ์„ ๋งŒ๋“œ๋Š”๋ฐ ๋‚ด๊ฐ€ ํ‹€๋ฆฐ ๋‹จ์–ด๋ฅผ ๋ชจ์œผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š”๋ฐ ๋‹จ์–ด ํ…์ŠคํŠธ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด 1. Business ์ด๋ ‡๊ฒŒ 1๊ณผ .์ด ์„ž์—ฌ์žˆ์–ด ์ด๊ฑฐ ๋นผ๊ณ  ๋‹จ์–ด ๊ทธ ์ž์ฒด๋งŒ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์—†์„๊นŒ๋ฅผ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€

(1.)์„ ์ž๋ฅด๋ฉด ๋˜๊ฒ ๋‹ค๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

์ž๋ฅด๋Š” ๋ฐฉ๋ฒ•์€ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์—์„œ ์ฒ˜์Œ ์ธ๋ฑ์Šค๋ฅผ ์•Œ์•„๋‚ด๊ณ  ๋ช‡๋ฒˆ์งธ๊นŒ์ง€ ์ž˜๋ผ์ฃผ๋ผ ํ•˜๋ฉด๋œ๋‹ค. ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด .index(๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค,์–ด๋””๊นŒ์ง€ ์ž˜๋ผ์•ผํ• ์ง€ ์ธ๋ฑ์Šค)๋ฅผ ์“ฐ๋ฉด๋œ๋‹ค.

let str = "1.asd"
let startIdx:String.Index = str.index(str.startIndex, offsetBy: 2)
print("\(str[startIdx...])") //asd๋ฅผ ์ถœ๋ ฅ

์•„๋ž˜๋Š” ์ ์šฉ๊ฒฐ๊ณผ

+๋ณด๋„ˆ์Šค

 let str = "1.asd"
 let findIdx:String.Index = str.firstIndex(of: ".")! //์›ํ•˜๋Š” ์ง€์ ์„ ์ •ํ•ด์„œ
 print("\(str[findIdx...])") // ์›ํ•˜๋Š” ์ง€์ ๋ถ€ํ„ฐ ๋๊นŒ์ง€ .asd๋ฅผ ์ถœ๋ ฅ
 print("\(str.prefix(3))")//์•ž์—์„œ 3๊ฐœ๋งŒ ๋ณด์—ฌ์ค˜ 1.a์ถœ๋ ฅ
 print("\(str.suffix(3))")//๋’ค์—์„œ 3๊ฐœ๋งŒ ๋ณด์—ฌ์ค˜ asd ์ถœ๋ ฅ
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€