Algorithm/프로그래머스

[프로그래머스 스위프트] 핸드폰 번호 가리기

devKen 2022. 8. 24. 10:37

문제 요약

뒷 네 자리만 제외하고 전부 마스킹하면 된다. 이를 위해서 startIndex의 위치를 알아내고 문자열의 길이에 맞춰 마스킹된 애스터리스크를 생성하여 출력하고 원본 숫자 네 자리를 출력한다. 

해답

func solution(_ phone_number:String) -> String {
    let startindex = phone_number.index(phone_number.endIndex, offsetBy: -4)
    return String(repeating: "*", count: phone_number.count-4) + phone_number[startindex...]
}