본문 바로가기
Swift

Swift String to Array (component)

by HaningYa 2020. 7. 3.
728x90

예제

import Foundation

var str = "1234 56789"
var arr = str.components(separatedBy: "5")
var arr2 = Array(str)
print(arr)
print(arr2)

let replacedStr = str.replacingOccurrences(of: " ", with: "")
var arr3 = replacedStr.components(separatedBy: "5")
print(arr3)

var eq = "1+2-3*4/5"
print(eq)
var eqArr = eq.components(separatedBy: ["+","-","*","/"])
                .map{(value:String) -> Int in return Int(value)! }
print(eqArr)
print(eqArr[0] + eqArr[1])

var name = "Hello HaningYa"
for index in name.indices {
    print(name[index], terminator: "" )
}
print()
for n in name {
    print(n, terminator: "" )
}

output

["1234 ", "6789"]
["1", "2", "3", "4", " ", "5", "6", "7", "8", "9"]
["1234", "6789"]
1+2-3*4/5
[1, 2, 3, 4, 5]
3
Hello HaningYa
Hello HaningYa

728x90

'Swift' 카테고리의 다른 글

Swift Set  (0) 2020.07.09
Swift 알고리즘에 쓰이는 문법 정리  (0) 2020.07.07
Swift 코딩 테스트 준비 - Java to Swift 😱  (1) 2020.07.01
[🗂 Swift Data Structure] - Stack  (0) 2020.06.25
스위프트 101 - Objects and Classes  (0) 2020.03.26

댓글