개발집사
2022. 6. 27. 15:46
str[index]
index로 접근은 가능하지만 쓸 수는 없음(read-only)
var str = 'CodeStates';
console.log(str[0]); //'C'
console.log(str[4]); //'S'
console.lgo(str[10]); //undefined
concatenating strings
- +연산자를 쓸 수 있음.
- string타입과 다른 타입 사이에 + 연산자를 쓰면, string 형식으로 변환(toString)
- str1.concat(str2, str3 ...);의 형태로도 사용가능
length PROPERTY
- 문자열 전체 길이를 반환
- str.length
str.indexOf(searchValue)
- arguments: 찾고자 하는 문자열
- return value: 처음으로 일치하는 index, 찾고자 하는 문자열이 없으면 -1
- lastIndexOf 는 문자열 뒤에서부터 찾음
'Blue Whale'.indexOf('Blue') //0
'Blue Whale'.indexOf('blue') //-1
'Blue Whlae'.indexof('Whale') //5
'canal'.lastIndexOf('a') //3
str.split(seperator)
- arguments: 분리 기준이 될 문자열
- return value: 분리된 문자열이 포함된 배열
- str.split(' ') // 공백 기준 띄어쓰기
- str.split('\n') //엔터 기준 띄어쓰기
str.substring(start, end)
- arguments: 시작 index, 끝 index
- return value: 시작과 끝 index 사이의 문자열
※음수는 0으로 취급
str.toLowerCase()/ str.toUpperCase()
- arguments: 없음
- return value: 대, 소문자로 변환된 문자열
What is immutable?
모든 string method는 immutable
즉, 원본이 변하지 않음
array method는 immutable 및 mutable 여부를 잘 기억해야함.