→ 문자열의 길이 를 나타낸다.
const string = "Hello World";
string.length //11
→ 매개변수로 전달된 모든 문자열을 호출한 문자열에 붙여서 새로운 문자열을 반환한다.
const str1 = 'Hello';
const str2 = 'World';
console.log(str1.concat(' ', str2)); //'Hello World'
console.log(str2.concat(' ~ ', str1)); //'World ~ Hello'
→ 특정 문자열로 끝나는지를 확인시켜준다.
true
/ false
const string = "it's gonna be alright!";
console.log(string.endsWith('gonna')); //false
console.log(string.endsWith('be')); //false
console.log(string.endsWith('alright')); //false
console.log(string.endsWith('alright!')); //true
→ 특정 문자열이 다른 문자열에 포함되어있는지 를 확인시켜준다.
true
/ false
const sentence = "it's gonna be alright!";
const word = 'star';
const word2 = 'alright';
console.log(sentence.includes(word)); //false
console.log(sentence.includes(word2)); //true
console.log(sentence.includes("be")); //true
→ 호출한 string 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환해준다.
index
-1
반환string.indexOf()
에 찾으려는 문자열 뿐만 아니라, 특정 index
값을 전달하면 그 index부터 찾는다!const sentence = "it's gonna be alright!";
const word = 'star';
const word2 = 'alright';
console.log(sentence.includes(word)); //false
console.log(sentence.includes(word2)); //true
console.log(sentence.includes("be")); //true
console.log(sentence.indexOf("Be")); //-1
→ 호출한 string 객체에서 주어진 값과 일치하는 부분을 뒤에서부터 역순으로 탐색하여 최초로 마주치는 인덱스를 반환해준다.
index
-1
반환const sentence = "it's gonna be alright! alright!";
console.log(sentence.lastIndexOf("alright")); //23
console.log(sentence.lastIndexOf("alright", 20)); //14
console.log(sentence.indexOf("alright")); //14
→ 문자열을 주어진 횟수만큼 반복해서 붙인 새로운 문자열을 반환한다.
"Hello".repeat(4); //'HelloHelloHelloHello'
const origin = "Welcome";
const repeat = origin.repeat(4);
console.log(origin); //'Welcome'
console.log(repeat); //'WelcomeWelcomeWelcomeWelcome'
→ 해당하는 부분의 문자열을 주어진 문자열로 변환시켜서 새로운 문자열을 반환한다.
indexOf
처럼 처럼 만나는 부분에 대해서 교체가 된다. const string = "I will always love you!";
const newString = string.replace("love", "like");
console.log(string);
console.log(newString);
→ 전달한 인덱스부터 endIndex 직전값까지 반환한다.
const string = "I will always love love you! ";
//endIndex 입력 안해주면 마지막까지 추출해준다.
const newString = string.slice(7);
//endIndex를 입력해주면, 입력해준 인덱스는 포함하지 않고 출력해준다..
const newString2 = string.slice(14,18);
console.log(string); //'I will always love love you! '
console.log(newString); //'always love love you! '
console.log(newString2); //'love'
→ String 객체를 지정한 구분자를 이용해서 여러 개의 문자열로 나눠 배열에 담아준다.
const string = "one,two,three,four,five";
const newArray = string.split(',');
console.log(string); //'one,two,three,four,five'
console.log(newArray); //[ 'one', 'two', 'three', 'four', 'five' ]