String Methods

  • string에서 bracket notation으로(index) 접근은 가능하지만 수정은 불가능함. (read only)
var str = 'hello world';
str[0] // => h
str[0] = 'g';
str; // => hello world

1. str.length

var str = 'hello world';
str.length // => 11

2. str.indexOf(searchValue)

  • argument : 찾고자 하는 문자열
  • return value : 처음으로 일치하는 index값. 찾고자하는 문자열이 없으면 -1 리턴
  • lastIndexOf는 뒤에서부터 찾음.
var str = 'hello world';
str.indexOf('o'); // => 4
str.lastIndexOf('o'); // => 7
/* 없는 문자열을 찾을 때 */
str.lastIndexOf('s'); // => -1

3. str.split(seperator)

  • arguments: 분리 기준이 될 문자열
  • return value: 분리된 문자열들이 들어있는 배열
var str = 'hello world';
str.split(' '); // => [hello, world]

4. str.substring(start, end)

  • arguments: 시작 index, 끝 index (끝 index는 포함되지 않음)
  • return value: 시작index~끝index-1 문자열
var str = 'hello world';
str.split(' '); // => [hello, world]

5. str.toLowerCase() / str.toUpperCase()

  • return value: 대소문자로 변환된 문자열
var str = 'Hello World';
str.toLowerCase(); // => "hello world"
str.toUpperCase(); // => "HELLO WORLD"

6. mutable / immutable

  • 모든 string method는 immutable
  • immutable : 원본이 변하지 않음
  • mutable : 원본이 변함