
String 전역 객체는 문자열(문자의 나열)의 생성자
String 객체 안에 값을 넣어 문자형으로 변환 가능
const text =new String('Hello world!');
console.log(typeof text); // object
console.log(text); //[String: 'Hello world!']
const text =new String('Hello world!');
console.log(text[2]); // l
String 객체의 인스턴스 메소드로 특정인덱스에 위치하는 문자열 반환
const text =new String('Hello world!');
console.log(text.charAt(2)); // l
둘다 인덱스 번호로 접근한다는 점에서 비슷하다.
문자열의 길이를 반환하며 오직 읽을 수 밖에 없다.
const text =new String('Hello world!');
console.log(text.length);
text.length=7; // 변경 x
console.log(text.length);
인자의 붙어있는 ?는 option을 의미한다.
String.prototype~
자주 쓰이는 메소드들 위주로
찾는 문자열이 전체 문자열 안에 포함되어 있는지 확인하고 true,false 반환
position은 문자열을 찾기 시작할 위치 ( 기본값 : 0)
includes는 대소문자를 구분한다.
const text =new String('Hello world!');
console.log(text.includes('h')); // false
console.log(text.includes('H')); // true
찾는 문자열의 인덱스 번호 반환, 찾지 못하면 -1 반환
fromindex의 인덱스 번호부터 찾기 시작 (기본값 :0 )
const text =new String('Hello world!');
console.log(text.indexOf('l',7)); // 9
찾는 문자열의 인덱스 번호 반환. 역순으로 탐색 , 찾지 못하면 -1 반환
const text =new String('Hello world!');
console.log(text.lastIndexOf('l',5)); //3
text 문자열을 인덱스가 5인 부분부터 역순 탐색
문자열이 특정 문자열로 시작하는지 확인하여 결과를 true false로 반환
position: 탐색을 시작할 인덱스 위치
const text =new String('Hello world!');
console.log(text.startsWith("Hello")) // true
문자열이 특정 문자열로 끝나는지 확인하여 결과를 true false로 반환
position: 탐색을 시작할 인덱스 위치
const text =new String('Hello world!');
console.log(text.startsWith("ld")) // true
문자열을 대문자로 변환해 반환
const text =new String('Hello world!');
console.log(text.toUpperCase()); // HELLO WORLD!
문자열을 소문자로 변환해 반환
const text =new String('Hello world!');
console.log(text.toLowerCase()); // hello world!
문자열의 시작인덱스부터 종료인덱스 전까지를 반환
indexStart 가 indexEnd와 같을 경우, 빈 문자열을 반환
const text =new String('Hello world!');
console.log(text.substring(1,9)); // ello wor
substring과 비슷하지만 새로운 문자열을 반환한다는 점이 다르다.
const text =new String('Hello world!');
console.log(text.slice(5,-2)); // worl
문자열 양 끝의 공백을 제거한 새로운 문자열을 반환
const text =new String(' Hello world!' );
console.log(text.trim()); // Hello world!
String 객체를 지정한 구분자를 이용해서 여러 문자열로 나눠 배열 생성
seperator : 사용할 구분자
limit: 문자열을 끊는 횟수를 지정
const text =new String('Hello world!');
console.log(text.split()); // [ 'Hello world!' ]
console.log(text.split(' ')); // [ 'Hello', 'world!' ]
console.log(text.split(''));
/* [
'H', 'e', 'l', 'l',
'o', ' ', 'w', 'o',
'r', 'l', 'd', '!'
]
*/
const text2 ="Hello World. How are you doing?";
console.log(text2.split(' ', 3);
// [ 'Hello', 'World.', 'How' ]
String 객체의 원시값 반환
const text =new String('Hello world!');
console.log(text); // [String: 'Hello world!']
console.log(text.valueOf()); // Hello world!
문자열이 정규식과 매치되는 부분을 검색합니다
console.log('00'.match(/[^0]/g)); // null
console.log('-1'.match(/[^0]/g)); // [ '-', '1' ]
console.log('10'.match(/[^0]/g)); // [ '1' ]
console.log('552'.match(/[^0]/g)); // [5,5,2]
문자열이 정규식과 일치하면, 일치하는 전체 문자열을 첫 번째 요소로 포함하는 Array를 반환한 다음 괄호 안에 캡처된 결과가 옵니다. 일치하는 것이 없으면 null이 반환됩니다.