Swift/with. APS
[Swift] 문자열에서 특정 문자 제거하거나 치환하기
아잉으니야
2021. 2. 10. 19:30
[Swift] 문자열에서 특정 문자 제거하거나 치환하기
python
에서는 문자열에서 특정 문자를 제거하거나 치환하기 위해서 replace
함수를 사용하면 된다.
name = "seonho"
print(name.replace("e", ""))
# sonho
print(name.replace("e", "a"))
# saonho
swift
에서도 이런 함수가 있는지 확인하기 위해 찾아보니 다행히 있었다!
replacingOccurrences(of:with:)
of
에는 바꾸고 싶은 문자, with
에는 바꿀 문자를 적어주면 된다.
let name: String = "seonho"
let change1: String = name.replacingOccurrences(of: "e", with: "")
let change2: String = name.replacingOccurrences(of: "e", with: "a")
print(change1)
/* sonho */
print(change2)
/* saonho */