자바 문자열 String Class 함수 replace()

2022. 10. 19. 13:48

replace() 는 문자열을 치환하는 함수 즉. 문자열에서 내가 바꾸고싶은 문자 또는 문자열을 바꿀수있다.
사용법 : (바꾸고자하는 문자열변수).replace("치환하고싶은 문자 또는 문자열","치환할 문자또는 문자열");
범위: replace() 는 문자 또는 문자열 모두 치환가능.

<예제>
String str1 = "hello world world"; // 문자열타입 str1 변수 선언. 변수에는 hello world world 문자열을 초기화

str1.replace("world", "WORLD");   // str1변수에 들어있는 문자열에서 "world" 문자열을 찾아 "WORLD" 로 바꿔라.
str1.replace('w', 'W'); // str1변수에 들어있는 문자열에서 'w' 문자를 찾아 'W' 로 바꿔라.

<결과>
String str1 = "hello world world";
str1.replace("world", "WORLD"); = hello WORLD WORLD
str1.replace('w', 'W'); = hello World World

replaceAll() 은 replace()와 모든기능은 동일하지만 사용범위가 다르다.
replace()  = '문자' , "문자열" 모두 사용가능
replaceAll()  = "문자열" 만 사용 가능, 또는 치환하고 싶은 문자열 범위를 정규표현식으로 지정도 가능!
사용법 : (바꾸고자하는 문자열변수).replace("치환하고싶은 문자열","치환할 문자열");

<예제>
static final String SMALL_LETTER_REGEX = "[a-z]"; // 정규표현식 선언 범위는 a에서 z까지 영문 소문자;
String strAll = "hello world world"; 
strAll.replaceAll("world", "WORLD"); // strAll 변수에 world문자열을 찾아 WORLD로 바꿔라.
strAll.replaceAll('w', 'W'); // strAll 변수에 'w' 문자를 찾아 'W'로 바꿔라!! 이렇게 하려면 replace()를 사용해야 한다.
                                        // 이건 안됨. (ERROR)
strAll.replaceAll(SMALL_LETTER_REGEX, "_");
                                       // strAll 변수에 정규표현식에 범위 해당하는 문자열을 모두언더바("_")로 바꿔라!!


<결과>
static final String SMALL_LETTER_REGEX = "[a-z]";
String strAll = "hello world world";
strAll.replaceAll("world", "WORLD"); = hello WORLD WORLD
strAll.replaceAll(SMALL_LETTER_REGEX, "_"); = _____ _____ _____

public class StringReplaceExample {
    static final String SMALL_LETTER_REGEX = "[a-z]";
    static final String NOT_ALNUM_REGEX = "[^a-z0-9]";
    static final String KOREAN_REGEX = "[가-힣]";
    public static void main(String[] args) {
        String str1 = "hello world world";
        String str2 = "hello world world";
        String replace1 = str1.replace("world", "WORLD");
        System.out.println("replace1 = " + replace1);
        String replace2 = str2.replace('w', 'W');
        System.out.println("replace2 = " + replace2);
        System.out.println();

        String strAll1 = "hello world world";
        String strAll2 = "hello world world";
        String strAll3 = "hello world world";
        String replaceAll1 = strAll1.replaceAll("world", "WORLD");
//        String replaceAll2 = strAll2.replaceAll('w', 'W'); // 문자 불가
        String replaceAll3 = strAll3.replaceAll(SMALL_LETTER_REGEX, "_"); // 정규 표현식 사용가능
        System.out.println("replaceAll1 = " + replaceAll1);
//        System.out.println("replaceAll2 = " + replaceAll2);
        System.out.println("replaceAll3 = " + replaceAll3);
        System.out.println();

        // 입력 데이터 형식 확인 (포맷 확인)
        String strAll4 = "abasdasd!!012345^~*#"; 
        String replaceAllRegEx1 = strAll4.toLowerCase().replaceAll(NOT_ALNUM_REGEX, ""); // 영문자와 숫자 이외 문자 제거
        System.out.println("replaceAllRegEx1 = " + replaceAllRegEx1);

        String strAll5 = "가나다라마바사ABCDEFG"; 
        String replaceAllRegEx2 = strAll5.replaceAll(KOREAN_REGEX, ""); // 한글 제거
        System.out.println("replaceAllRegEx2 = " + replaceAllRegEx2);
        System.out.println();
    }
}

BELATED ARTICLES

more