๐ String
๊ตฌ์กฐ์ฒด์ด๊ณ ๋ค๋ฅธ ์ธ์ด์ ๊ฐ์ด character๊ฐ ๋ชจ์ธ ์งํฉ
Array() : ๋ฌธ์์ด -> ๋ฐฐ์ด
๋ฌธ์์ด์ ์์ ํ๋ํ๋๋ฅผ ๋ฐฐ์ด๋ก ๋ง๋๋ ๋ฐฉ๋ฒ.
let name: String = "SWIFT"
let arrName = Array(name)
print(arrName) // ["S","W","I","F","T"]
count() , empty() : ๋ฌธ์์ด์ ๊ธธ์ด ๊ตฌํ๊ธฐ
let string: "A Unicode string value that is a collection of characters."
print(string.count) // 58
let empty = ""
print(empty.isEmpty) // true
print(string.isEmpty) // false
append() : ๋ฌธ์์ด ํฉ์น๊ธฐ
๋ฌธ์์ด์ ํฉ์น๋ ๋ฐฉ๋ฒ์ ๊ฐ๋จํ๊ฒ 2๊ฐ์ง๊ฐ ์๋ค.
var str = "Swift"
str.append(" is Hard.")
print(str) // "Swift is Hard.
var str1 = "ABC"
var str2 = "DEFG"
print(str1 + str2) // "ABCDEFG"
๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ ( ๋๋ฌธ์, ์๋ฌธ์, ์ซ์์ธ์ง, ๋ฌธ์์ธ์ง ํ์ )
let str = "lower"
print(str.uppercased()) // "LOWER"
let str = "UPPER"
print(str.lowercased()) // "upper"
let char = "a" as Character
print(char.isNumber) // false
print(char.isLetter) // true
print(char.isUppercase) // false
print(char.isLowercase) // true
print(char.isASCII) // true
print(char.asciiValue) // Optional(97) -> ์ธ๋ํ ํด์ค์ผํ๋ค.
let num = "1"
print(num.isNumber) // true
๋ฌธ์์ด์ ์์๊ณผ ๋
์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ํ๋ค๋ณด๋ฉด ์ข ์ข ๋ฌธ์์ด์ ๋งจ ์ฒ์๊ณผ ๋์ ๋ค๋ฃจ๊ฒ ๋๋ ๊ฒฝ์ฐ๊ฐ ์ข ์ข ์๋ค. ๋ ๊ทธ ๋ฌธ์๊ฐ ๋ฌธ์์ด์ ์กด์ฌํ๋์ง ์ฌ๋ถ๋ ์ ์ ์๋ ํจ์๊ฐ ์๋ค.
var str = "ABCDE"
print(str.hasPrefix("A")) // true -> ์ ๋์ด ํ๋จ
print(str.hasSuffix("E")) // true -> ์ ๋ฏธ์ด ํ๋จ
print(str.contains("A")) // true -> ์กด์ฌ ํ๋จ
print(str.first) // Optional("A")
print(str.last) // Optional("E")
.first , last ํจ์๋ฅผ ์ฌ์ฉํ ๋์๋ ์ต์ ๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค. ๋ฐ๋ผ์ ์ธ๋ํ์ ํด์ค์ผํ๋ค.
init(repeating:count:) : ๋ฌธ์ ๋ฐ๋ณต
String๊ณผ Array์์ ์ฌ์ฉํ ์ ์๋ค. ๋ฐฐ์ดํฌ๊ธฐ์ ์ด๊ธฐ๊ฐ์ ๋ฃ์ด์ค ์ ์์ผ๋ฉฐ, ํน์ ๊ฐ์ ๋ฐ๋ณตํ์ฌ ๊ฐ์ ๋ง๋ค ์ ์๋ค.
repeating์ ๋ฐ๋ณตํ ๋ฌธ์, ๋ฌธ์์ด, ์ซ์, ๋ถ์ธ ๋ฑ์ ๋ฃ์ด์ฃผ๋ฉด ๋๊ณ , count์๋ ๋ฐ๋ณต ํ์๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
String
let str = String(repeating:"str", count:3)
print(str) // strstrstr
let ch = Character(readLine()!) // G
print(String(repeating: ch, count:4) / GGGG
Array
// [Int](repeating: 0, count:5)์ ๊ฐ์
let in = Array(repeating:0, count:5)
print(in) // [0,0,0,0,0]
let in = [String](repeating:"c", count:5)
print(in) //["c","c","c","c","c"]
'๐ ์ฝ๋ฉํ ์คํธ > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ReplacingOccurrences ( ๋ฌธ์์ด ์นํ ) (0) | 2023.11.02 |
---|---|
[Swift] Split , Components (0) | 2023.11.02 |
[Swift] Map, Filter, Reduce (1) | 2023.11.01 |
[Swift] prefix suffix (0) | 2023.11.01 |
[Swift] ๋ฐฐ์ด ๋ฉ์๋ sort() sorted() / reverse() reversed() (0) | 2023.10.18 |