코딩 테스트/프로그래머스

[프로그래머스 입문]

감귤밭호지차 2023. 4. 28. 12:58

# 특정 문자 제거하기

function solution(my_string, letter){
	const answer = my_string.split(letter).join('');
    return answer;
}

굳이 my_string을 split( ' ' )으로 나눌 필요 없이 빼야 하는 요소 letter을 기준으로 분리 한다음에 join( )으로 합칠 수 있었습니다. 

.

.

function solution(my_string, letter){
	return my_string.replaceAll(letter, "");
}​

replaceAll( )
replace( ) 메소드를 사용하면 일부 문자열을 바꿀 수 있습니다.
replaceAll( ) 메소드를 사용하면 전체 문자열에서 일괄적으로 대체할 수 있습니다. 

const str = 'hello world';
const newStr = str.replaceAll( '1', 'X'); 

' l ' 요소를 ' X ' 로 바꿀 수 있습니다.