Javascript - 문자열

ㅂㅈㄷㅂ123·2022년 6월 27일
0
post-thumbnail

문자열이란?

  • 일상생활에서 확인할 수 있는, 모든 글자의 나열

str[index]

var str = 'coco mong'
console.log(str[0]) // 'c'
console.log(str[4]) // 'o'
console.log(str[5] // ' '
  • '+'연산자 쓸 수 있음
  • string 타입과 다른 타입 사이에 + 연산자를 쓰면, string 형식으로 변환(toString)

length PROPERTY

  • 문자열 전체 길이를 반환

str.indexOf(searchValue)

'Blue White'.indexOf('Blue') // 0
'Blue White'.indexOf('blue') // -1
'Blue White'.indexOf('White') // 5
'Blue White White'.indexOf('White') // 5
'Blue White'.lastIndexOf('e') // 9 

str.includes(searchValue)

'Blue White'.includes('blue') //false
'Blue White'.includes('Blue') //true

str.split(seperator)

var str = 'Hello from the other side';
console.log(str.split(' '));
// ['Hello', 'from', 'the', 'other', 'side']
str.split('\n') //한 줄 띄기에 스플릿
str.split(',') //쉼표에 스플릿

str.substring(start, end)

var str = 'abcdefghij';
console.log(str.substring(0, 3)); // 'abc'
console.log(str.substring(3, 0)); // 'abc'
console.log(str.substring(1, 4)); // 'bcd'
console.log(str.substring(-1, 4)); // 'abcd', '음수는 0으로 취급'
console.log(str.substring(0, 20)); //'abcdefghij, index 범위를 
벗어나는 값은 나오지 않는다. 만약 자바라면 오류가 나온다

str.toLowerCase()

console.log('ABCDE'.toLowerCase()); //'abcde'

str.toUpperCase()

console.log('abcde'.toUpperCase()); //'ABCDE'

IMMUTABLE

let word = 'hello';
word.toUpperCase() //'HELLO'
----------------------------
그럼 word는 바뀌었을까?
word =>'hello'가 나온다
  • 모든 string method는 immutable. 즉, 원본이 변하지 않음
  • array method는 immutable 및 mutable 여부를 잘 기억해야 함
profile
ㅂㅈㄷㅂㅈㄷ

0개의 댓글

관련 채용 정보