1. 메서드란 ?
ㆍ 자바스크립트 메서드는 객체에서 수행할 수 있는 작업 입니다.
ㆍ 메서드(Method)는 함수 정의(function definition)를 포함하는 속성(property) 입니다.
1) str[index] : index로 접근은 가능하지만 사용은 불가능합니다.(읽기만 가능)
let str = 'abcd';
console.log(str[0]); // 'a'
console.log(str[1]); // 'b'
2) + 연산자 : string 타입과 다른 타입 사이에 + 연산자를 사용하면, string 타입으로 변환합니다.
let str1 = 'abcd';
let str2 = 'efgh';
let str3 = '1';
console.log(str1 + str2); // 'abcdefgh'
console.log(str3 + 8); // '18'
3) .length : 문자열의 전체 길이를 반환합니다.
let str = 'abcd';
console.log(str.length); // 4
ㆍ 메서드(Method)는 함수 정의(function definition)를 포함하는 속성(property) 입니다.
4) str.indexof : 찾고자하는 문자열의 위치를 반환합니다.
'Steve Jane'.indexof('Steve'); // 0
'Steve Jane'.indexof('steve'); // -1 (찾고자 하는 문자열이 없으면 -1을 반환합니다.)
'Steve Jane'.indexof('Jane'); // 5
5) str.split() : 문자열을 분리하는데 사용합니다.
let str = 'a b c d';
console.log(str.split(' ')); // ['a', 'b', 'c', 'd']
- 분리 된 문자열은 배열로 반환됩니다.
6) str.substring(start, end) : 시작과 끝 index 사이의 문자열을 반환합니다.
let str = 'abcde';
console.log(str.substring(0, 3); // 'abc' (끝 index 가 3 이지만, 2 까지만 반환합니다.)
console.log(str.substring(2, 4); // 'cd'
7) str.toLowerCase() / str.toUpperCase() : 소문자, 대문자로 변환합니다.
console.log('ABCDEF'.toLowerCase()); // 'abcdef'
console.log('abcdef'.toUpperCase()); // 'ABCDEF'
이상으로 문자열 메서드를 알아보았습니다.
'JavaScript > 1' 카테고리의 다른 글
배열 다루기(Array Methods) (0) | 2020.03.31 |
---|---|
반복문(for 문, for..in 문, for..of 문, while 문) (0) | 2020.03.31 |
배열(Arrary)과 객체(Object) (0) | 2020.03.30 |
조건문(Conditional)과 함수(Function) (0) | 2020.03.24 |
변수(Variable)와 타입(Type) (0) | 2020.03.24 |