티스토리 뷰

반응형

코딩 테스트에서 유용하게 사용되는 메서드 중 하나가 문자열 처리 메서드이다. 특히 KAKAO에서는 문자열 처리 문제를 많이 내기 때문에 정리해두는 것이 좋다.

 

 

출제된 문자열 처리 문제

[2018 KAKAO BLIND RECRUITMENT] 다트게임

[2019 카카오 개발자 겨울 인턴십] 불량 개발자

[2019 KAKAO BLIND RECRUITMENT] 매칭 점수

[2021 KAKAO BLIND RECRUITMENT] 신규 아이디 추천

 

 

 

 

 

 

 

 

 

 

 

구분 메서드
대소문자 변환 toUpperCase(), toLowerCase()
문자열 대체 replace()
문자열 포함 여부 includes()
문자열 위치 찾기 indexOf(), lastIndexOf()
문자열 나누기 split()
문자열 합치기 concat()
특정 위치의 문자열 반환 charAt()
문자열 공백 제거 trim()
문자열 추출 slice(), substring(), substr()

 

 

 

 

대소문자 변환

toUpperCase()

문자열을 모두 대문자로 변형하여 새 문자열로 반환한다.

toUpperCase()

 

예제

const str = 'Hello, world!';
const newStr = str.toUpperCase();

console.log(str); // "Hello, world!"
console.log(newStr); // "HELLO, WORLD!"

 

 

 

toLowerCase()

문자열을 모두 소문자로 변형하여 새 문자열로 반환한다.

toLowerCase()

 

예제

const str = 'Hello, world!';
const newStr = str.toLowerCase();

console.log(str); // "Hello, world!"
console.log(newStr); // "hello, world!

 

 

 

 

 

 

 

문자열 대체

replace()

정규 표현식이나 부분 문자열을 이용하여 문자열을 검색하고 대체해준다.

replace는 대체하려는 문자열이 여러 개 존재하는 경우 첫 번째로 발견한 문자열만 대체해준다.

 

str.replace(정규식or문자열, 새문자열or함수)

 

예제

- 부분 문자열

const str = 'aaaaabbbbb';
const newStr = str.replace('a', 'c');

console.log(newStr); // "caaaabbbbb"

 

- 정규식

const str = 'aaaaabbbbb';
const newStr = str.replace(/b/gi, 'f');
// /b/gi : b 문자 전체를 의미

console.log(newStr); // "aaaaafffff"

 

 

 

 

 

 

 

 

문자열 포함 여부

includes()

특정 문자열이 포함되어 있는지 확인하고 bullean으로 반환한다.

includes(문자열, 시작할인덱스)

 

 

예제

const str = 'aaaaabbbbb';
const inc = str.includes("b");

console.log(inc); // "true"

 

 

 

 

 

문자열 위치 찾기

indexOf()

찾으려는 문자열의 첫 번째 인덱스를 반환하고, 문자열이 없으면 -1을 반환한다.

indexOf(문자열)

 

예제

const str = 'aaaaabbbbb';
const idx = str.indexOf("b");

console.log(idx); // 5

 

 

 

lastIndexOf()

찾으려는 문자열의 마지막 인덱스를 반환하고, 문자열이 없으면 -1을 반환한다.

lastIndexOf(문자열)

 

예제

const str = 'aaaaabbbbb';
const idx = str.lastIndexOf("b");

console.log(idx); // 9

 

 

 

 

문자열 나누기

split()

split(구분자, 추출할글자수)

 

예제

const str = "Hello world";

const a = str.split(); // ["Hello world"]
const b = str.split(''); // ["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
const c = str.split('', 3); // ["H", "e", "l"]
const d = str.split(' '); // ["Hello", "world"]
const e = str.split('l'); // ["He", "", "o"]

 

 

 

 

문자열 합치기

concat()

concat은 문자열이나 배열을 하나로 합쳐 반환한다.

concat(합칠문자열)

 

예제

const str1 = 'Hello, ';
const str2 = 'world';
const str = str1.concat(str2);

console.log(str); // "Hello, world"

 

 

 

특정 위치의 문자열 반환

charAt()

특정 인덱스에 위치하는 문자열을 반환한다.

charAt(index)

 

예제

const str = "Hello, world";
const letter = str.charAt(3);

console.log(letter); // "l"

 

 

 

 

문자열 공백 제거

trim()

trim을 사용하면 문자 양 끝에 있는 공백을 제거할 수 있다.

trim()

 

 

예제

const str = "      Hello, world       ";
const newStr = str.trim();

console.log(newStr); // "Hello, world"

 

 

 

 

문자열 추출

slice()

slice는 start부터 end 인덱스 전까지 추출하여 새로운 문자열로 반환한다. 원본은 수정되지 않는다.

slice(start, end)

 

예제

const str = "Hello, world";
const newStr = str.slice(0, 5);

console.log(newStr); // "Hello"

 

 

 

substring()

slice와 마찬가지로 start부터 end 인덱스 전까지 추출하여 새로운 문자열로 반환한다. 원본을 수정되지 않는다.

substring은 음수 인덱스이거나 start 인덱스가 end보다 클 경우 빈 문자열을 반환한다.

substring(start, end)

예제

const str = "Hello, world";
const newStr = str.substring(0, 5);

console.log(newStr); // "Hello"

 

 

slice와 substring 차이점

 

 

 

substr()

substr은 start 인덱스부터 문자 개수만큼 잘라서 반환한다.

substr(start, 문자수)​

 

 

예제

const str = "Hello, world";
const newStr = str.substr(0, 3);

console.log(str); // "Hello, world"
console.log(newStr); // "Hel"

 

 

 

 

 

 

반응형
반응형
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31