문제 : 핸드폰 번호 가리기
프로그래머스 모바일은 개인정보 보호를 위해 고지서를 보낼 때 고객들의 전화번호의 일부를 가립니다.
전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자를 전부 *으로 가린 문자열을 리턴하는 함수, solution을 완성해주세요.
- 제한 조건 : phone_number는 길이 4 이상, 20이하인 문자열입니다.
- 입출력 예
phone_number, return
"01033334444" "***4444"
"027778888" "*8888"
풀이
func solution(_ phone_number:String) -> String {
return String(repeating: "*", count: phone_number.count - 4) + phone_number.suffix(4)
}
repeating count
-특정 문자열을 지정한 갯수만큼 다른 문자로 변경하고 싶은 경우 사용되는 내장함수.
suffix(_:)
-Returns a subsequence, up to the given maximum length, containing the final elements of the collection.
-마지막 부터 count