프로그래머스/레벨1

[프로그래머스_자바] 신규 아이디 추천

박차 2021. 7. 1. 17:19

 

 

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
public String solution(String new_id) {
 
        // 1단계(소문자로 변경)
        String one = new_id.toLowerCase();
 
        // 2단계(정규화)
        String two = one.replaceAll("[^-_.a-z0-9]""");
 
        // 3단계(점 변경)
        String three = two.toString().replace(".."".");
        while (three.contains("..")) {
            three = three.replace(".."".");
        }
 
        // 4단계(처음 혹은 끝에 점 제거)
        String four = three;
        String check1 = three.substring(01); // 처음 점
        if (check1.equals(".")) {
            four = four.substring(1, four.length());
        }
 
        if (four.equals(null)) {
            String check2 = four.substring(four.length() - 1); // 끝 점
            if (check2.equals(".")) {
                four = four.substring(0, four.length() - 1);
            }
        }
 
        // 5단계(빈 문자열 확인)
        String five = four;
        if (five.length() == 0) {
            five = "a";
        }
 
        // 6단계(문자 길이 자르기)
        String six = five;
        if (six.length() >= 16) {
            six = six.substring(015);
        }
 
        String check3 = six.substring(six.length() - 1); // 끝 점 제거
        if (check3.equals(".")) {
            six = six.substring(0, six.length() - 1);
        }
 
        // 7단계
        String seven = six;
        if (seven.length() < 3) {
            String lastPoint = seven.substring(seven.length() - 1);
            while (seven.length() < 3) {
                seven += lastPoint;
            }
        }
 
        return seven;
    }
cs