자바 스크립트 (JavaScript)/JavaScript Language Fundamentals
String Methods
종우공간
2021. 7. 24. 06:07
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