메서드에 들어가기 앞서, String은 배열과 마찬가지로 length 속성을 통해 문자열의 길이값을 구할 수 있다.
array메서드를 정리한 내용도 함께 보자!
문자열을 대문자로, 소문자로 바꿔준다
let lastName = 'Yeri Kim';
let upperLastName = lastName.toUpperCase();
let lowerLastName = lastName.toLowerCase();
console.log(lastName); // Yeri Kim
console.log(upperLastName); // YERI KIM
console.log(lowerLastName); // yeri kim
텍스트를 잘라준다
slice(잘릴 시작위치, 잘릴 끝위치);
⭐️ 잘릴 끝위치의 문구는 포함하지 않는다!⭐️
slice(0,12);라고 하면, 11번째 문구까지 잘라준다.
해당 문자열이 있는지 true/false
let str = "Hello world, welcome to the universe.";
str.includes("world") // Returns true
해당 문자열의 인덱스값을 반환해준다
만약, 해당 문자열이 없을 시에 -1이 반환된다.
let str = "Hello world, welcome to the universe.";
str.indexOf("welcome") // Returns 13
str.indexOf("Welcome") // Returns -1
해당 문자열을 새로운 문자열로 바꿔준다
let str = "Visit Microsoft!";
str.replace("Microsoft", "W3Schools");
Object.keys : 어떤 객체가 가지고 있는 키들의 목록을 배열로 반환한다.
const obj = {
name: 'melon',
weight: 4350,
price: 16500,
isFresh: true
}
Object.keys(obj) // ['name', 'weight', 'price', 'isFresh']
Object.values, Object.entries
const values = Object.values(obj)
// values === ['melon', 4350, 16500, true]
const entries = Object.entries(obj)
/*
entries === [
['name', 'melon'],
['weight', 4350],
['price', 16500],
['isFresh', true]
]
*/
메서드 정리를 하다보니 배열 메서드랑 겹치는게 많았다! 베열 메서드에서만 쓰일 수 있는 것, 문자열에서만 쓰일 수 있는것 잘 구분하자 ! 💪🏻