Jam's story
[신규 아이디 추천] 본문
replaceAll 로 지우는데,
지우려면 "" 공백이 없어야하는데 " " 으로 적어서 틀렸었다 .
^가 [] 안에 들어와있다면(예) [^0-9] 제외하고 라는 뜻이고
[] 밖에 있다면 예) ^[a] 시작하는 이란 뜻이다 .
charAt()으로 마지막 글자를 구하려면,
charAt(변수명.length()-1)
public class Solution {
public String solution(String new_id) {
//1단계:
new_id=new_id.toLowerCase();
//2단계
new_id=new_id.replaceAll("[^-_.a-z0-9]", "");
//3단계
new_id=new_id.replaceAll("[.]{2,}", ".");
//4단계
new_id=new_id.replaceAll("^[.]|[.]$", "");
//5단계
if(new_id.equals("")) {new_id+="a";}
//6단계
if(new_id.length()>=16) {
new_id=new_id.substring(0,15);
new_id=new_id.replaceAll("[.]$", "");
}
//7단계
if(new_id.length()<=2) {
while(new_id.length()<3) {
new_id+=new_id.charAt(new_id.length()-1);
}
}
return new_id;
}
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[숫자 문자열과 영단어] (0) | 2022.06.13 |
---|---|
[키패드 누르기] (0) | 2022.05.30 |
[숫자 문자열과 영단어] (0) | 2022.05.30 |
[신고 결과 받기] (0) | 2022.05.26 |
[로또의 최고 순위와 최저 순위] (0) | 2022.05.26 |