종우의 컴퓨터 공간
String Methods 본문
const firstName = 'William';
const lastName = 'Johnson';
const tags = 'web design,web development,programming';
length (property; no paranthesis)
console.log(firstName.length); //yields 7
concat(<str value>, <str value2>, ...)
console.log(firstName.concat(' Smith')); // yields "William Smith"
toUpperCase(<str value>), toLowerCase(<str value>)
console.log(firstName.toUpperCase()); //yields "WILLIAM"
console.log(firstName.toLowerCase()); //yields "william"
indexOf(<str value>)
- returns the start index of the str value
console.log(firstName.indexOf('l')); //yields 2
charAt(<number with str value>)
- returns the char in that index
console.log(firstName.charAt(0)); //yields "w"
substring(<index>, <index>)
console.log(firstName.substring(0, 4)); //yields "will"
slice(<index>, ...)
- we can use the negative index which counts from backward
console.log(firstName.slice(0, 4)); //yields "will"
console.log(firstName.slice(-3)); //yields "iam"
split(<str value>)
- split strings in arrays with provided str value
console.log(tags.split(',')); //yields ["web design", "web development", "programming"]
replace(<str value>, <str value>)
console.log(firstName.replace("will", "James")); //yields "Jamesiam"
includes(<str value>)
- returns true if str value is included in the string or false otherwise
console.log(tags.includes("web development"); //yields true
console.log(tags.includes("foo")); //yields false
'자바 스크립트 (JavaScript) > JavaScript Language Fundamentals' 카테고리의 다른 글
Arrays & Array Methods (0) | 2021.07.25 |
---|---|
템플릿 문자열(Template Literals) (0) | 2021.07.24 |
The Math Object (0) | 2021.07.24 |
형 변환(Type Conversion) (0) | 2021.07.24 |
데이터 타입(data types) (0) | 2021.07.23 |