코딩 테스트/TIP

[JAVA] 문자열이 영어로만 이루어져 있는지 판별하기(Pattern.mathces())

갓생사는 김초원의 개발 블로그 2020. 11. 10. 21:25

문자열이 알파벳으로만 이루어져있는지 확인할 때 사용한다. 

숫자, 한글, 특수문자 등이 한글자라도 섞여있으면 false를 반환한다. 

 

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String s1 = "chocholog";
        System.out.println(Pattern.matches("^[a-zA-Z]*$", s1));

        String s2 = "chocho_log";
        System.out.println(Pattern.matches("^[a-zA-Z]*$", s2));
    }
    
    //
    
    true
    false