-1
을 return 한다.const str = 'this is sentence';
str.indexOf('this'); // 0
str.indexOf('are'); // -1
const str = 'this is sentence';
const is = str.slice(5, 7);
console.log(is); // is
substring과 slice의 차이점
start > end
일 경우, substring은 이를 바꾸어 결과를 계산한다.let str = 'Hello World';
let a = str.substring(5, 0);
let b = str.slice(5, 0);
console.log('a: ' + a);
console.log('b: ' + b);
start
가 음수일 경우, substring은 이를 0으로 변경하여 계산한다.let str = 'Hello World';
let a = str.substring(-8, 10); // (0, 10)
let b = str.slice(-8, 10); // (3, 10)
let c = str.substring(-8); // (0)
let d = str.slice(-8); // (3)
console.log(a); // Hello Worl
console.log(b); // lo Worl
console.log(c); // Hello World
console.log(d); // lo World
const str = 'aPple';
str.toUpperCase(); // APPLE
str.toLowerCase(); // apple
const str = 'ABCDE';
str.startWith('A'); // true
str.endWith('F'); // false
str.endWith('BC', 3); // true, str의 첫 3개 문자인 ABC만을 고려하여 판단.
const str = 'ABCDE';
str.includes('AB'); // true
str.includes('EF'); // false
const str = 'ABCDE';
str.repeat(3); //ABCDEABCDEABCDE
const greeting = ' Hello world! ';
console.log(greeting); // " Hello world! "
console.log(greeting.trimStart()); // "Hello world! "
replace()
의 경우 오직 첫 번째로 조건을 만족하는 문자열을 변경하고 나머지는 그대로 둔다.replaceAll()
의 경우 조건을 만족하는 문자열을 전부 변경시킨다.const test = 'My name is Baik gwangin';
const test2 = 'dog dog dog dog';
test.replace('Baik', 'Kim'); // 'My name is Kim gwangin'
test2.replaceAll('dog', 'cat'); // cat cat cat cat
const sentence = 'lazy dog.';
const index = 3;
sentence.charAt(index); // y
const str = 'The quick brown fox';
const words = str.split(' '); // ['The', 'quick', 'brown', 'fox']
console.log(words[3]); // fox