728x90
String 특정 기준으로 나누기 String -> [String] : components
var str = "12345678"
var arr = str.components(separatedBy : "5")
//["1234","678"]
String 빈칸 없애기 or 문자 치환 : replacingOccurrences
var str = "1 2 3 4 5"
let replaced = str.replacingOccurrences(of: " ", with: "")
//"12345"
String -> [String] : Array
var str = "123 45"
var arr = Array(str)
//["1","2","3"," ","4","5"]
[Int] -> [String] : map
//digits : [Int]
var numberString : String = digits.map{String($0)}
//[1,2,3,4,5]
//["1","2","3","4","5"]
[Int] -> String : joined
//digits : [Int]
var numberString : String = digits.map{String($0)}.joined(separator: "")
//[1,2,3,4,5]
//"12345"
728x90
'Swift' 카테고리의 다른 글
Swift enum - 열거형 (0) | 2020.07.09 |
---|---|
Swift Set (0) | 2020.07.09 |
Swift String to Array (component) (0) | 2020.07.03 |
Swift 코딩 테스트 준비 - Java to Swift 😱 (1) | 2020.07.01 |
[🗂 Swift Data Structure] - Stack (0) | 2020.06.25 |
댓글