String

less than 1 minute read

String

정규표현식

import java.util.*;

class Solution {
  public boolean solution(String s) {
        if (s.length() == 4 || s.length() == 6) return s.matches("(^[0-9]*$)");
        return false;
  }
}

String.mathces를 사용해서 “([])”

  • ^[0-9]*$ 숫자
  • ^[a-zA-Z]*$ 영문자
  • ^[가-힣]*$ 한글
  • \w+@\w+\.\w+(\.\w+)? E-Mail
  • ^\d{2,3}-\d{3,4}-\d{4}$ 전화번호
  • ^01(?:0 1 [6-9])-(?:\d{3} \d{4})-\d{4}$ 휴대전화번호
  • \d{6} - [1-4]\d{6} 주민등록번호
  • ^\d{3}-\d{2}$ 우편번호

isDigit()

class Solution {
  public boolean solution(String s) {
      boolean answer = false;
      if(s.length() == 4 || s.length() == 6 ){
        int i;
        for(i=0;i<s.length();i++){
           if(!Character.isDigit(s.charAt(i))){
               break;
           }
         }
        if(i==s.length()){
            answer =true;
        }
      }
      return answer;
  }
}

Character.isDigit() 숫자 = true반환, 문자데이터 false반환.

Updated: