Javascript

String 관련 method

자라나라나무나무나 2022. 5. 12. 23:51

indexOf

: 앞에서 부터 입력한 문자열의 자리를 index로 찾아주는 기능

 

indexOf(" ", )

: 지정 한 숫자 index 뒤 부터 세어라 (띄어쓰기 포함)

let str = "hello, world!!";

console.log(str.indexOf("l")); // output : 2
console.log(str.indexOf("l", 5)); // output : 10

 

lastIndexOf()

: 뒤에서 부터

let str = "hello, world!!";

console.log(str.lastIndexOf("r")); // output : 9

 

includes()

: () 안의 문자가 포함되어 있는지 (대소문자 구별하여)

let str = "hello, world!!";

console.log(str.includes("hello")); // output : true

 

startsWith()

: 시작하는 문자가 () 와 일치한가

let str = "hello, world!!";

console.log(str.startsWith("ello")); // output : false
console.log(str.startsWith("hello")); // output : true

 

endsWith()

: 뒤에서 부터 문자열이 () 와 일치한가

let str = "hello, world!!";

console.log(str.endsWith("!!!")); // output : false
console.log(str.endsWith("!!")); // output : true