으니의 개발로그

[Swift] 앞뒤 공백 제거하기 / 앞뒤의 특정 문자 제거하기 본문

Swift/with. APS

[Swift] 앞뒤 공백 제거하기 / 앞뒤의 특정 문자 제거하기

아잉으니야 2021. 2. 10. 20:00

[Swift] 앞뒤 공백 제거하기 / 앞뒤의 특정 문자 제거하기

 

보통의 프로그래밍 언어에서는 앞뒤의 공백을 제거 할 수 있는 trim 함수가 있고, python 에서는 앞뒤에 있는 특정문자를 제거할 수 있는 strip 함수가 있다.

그리고 swift 에서 사용하는 trim 함수는 pythonstrip 함수와 같은 기능을 할 수가 있다!!! 

 

 

trimmingCharacters(in:)

앞뒤 공백 제거하기

let name: String = "   seonho  "
print(name.trimmingCharacters(in: .whitespaces))
/* seonho */

 

앞뒤의 특정문자 제거하기

let name: String = ".seonho."
print(name.trimmingCharacters(in: ["."]))
/* seonho */

let favoriteColor: String = "yellow__"
print(favoriteColor.trimmingCharacters(in: ["_"]))
/* yellow */