-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path신규 아이디 추천.swift
More file actions
60 lines (48 loc) · 1.43 KB
/
신규 아이디 추천.swift
File metadata and controls
60 lines (48 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// 신규 아이디 추천.swift
//
//
// Created by chihoooon on 2022/01/04.
//
import Foundation
func removeSpecialCharacters(_ str: String) -> String {
var removedString = ""
for chr in str {
if chr.isLetter || chr.isNumber || chr == "-" || chr == "_" || chr == "." {
removedString.append(chr)
}
}
return removedString
}
func removeContinuousDots(_ str: String) -> String {
var removedString = str
while removedString.contains("..") {
removedString = removedString.replacingOccurrences(of: "..", with: ".")
}
return removedString
}
func setLengthToFifteen(_ str: String) -> String {
var removedString = str
if removedString.count >= 16 {
let index = removedString.index(removedString.startIndex, offsetBy: 15)
removedString = String(removedString[removedString.startIndex..<index])
if removedString.hasSuffix(".") {
removedString.removeLast()
}
}
return removedString
}
func solution(_ new_id:String) -> String {
var newID = removeSpecialCharacters(new_id)
newID = newID.lowercased
newID = removeContinuousDots(newID)
newID = newID.trimmingCharacters(in: ["."])
if newID.isEmpty {
newID += "a"
}
newID = setLengthToFifteen(newID)
while newID.count <= 2 {
newID.append(newID.last!)
}
return newID
}