Part. 3 문자열 다루기

Angelo·2020년 4월 20일
0

Codestates PRE Javascript

목록 보기
3/15
post-thumbnail

릴러말즈-두 개였으면 해

문자열 다루기

str [index]

	var str = 'CodesStates';
	console.log(str[0]);  // 'C'
	consoile.log(Str[4]); // 'S'
// index로 접근은 가능하지만 쓸 수는 없음 (read-only)

'+' 연산자

string 타입과 다른 타입 사이에 + 연산자를 쓰면, string 형식으로 변환 (toString)

	var str1 = '1';
	console.log(str1 + 7);  // 17

lengthProperty

문자열의 전체 길이를 반환

	console.log(str.length);  // 10

str.indexof (searchValue)

arguments : 찾고자 하는 문자열
return value : 처음으로 일치하는 index, 찾고자 하는 문자열이 없으면 -1
lastIndexOf는 문자열 뒤에서 부터 찾음

	‘Blue Whale’.indexof(‘Blue’);   // 0번째
	‘Blue Whale’.indexof(‘blue’);   // -1
	‘Blue Whale’.indexof(‘Whale’) ;  // 5번째 
	‘Blue Whale Whale’ .indexof(‘Whale’); //중복은 앞의 값만
	‘canal’.lastIndexOf ( ‘a’) ;   // 3번째 

str.split (separator)

arguments : 분리 기준이 될 문자열
return value : 분리된 문자열이 포함된 배열

	var str = 'Hello from the other side’;
	console.log(str.split(‘ ‘));
	// [‘Hello’ , ‘from’, ‘the’, ‘other’, ‘side’]

str.substring (start, end)

arguments : 시작 index, 끝 index
return value : 시작과 끝 index 사이의 문자열

	console.log(str.substring (0, 3)); // ‘abc’
	console.log(str.substring (3, 0)); // ‘abc’
	console.log(str.substring (-1,4)); // ‘abcd’. 음수는 0으로취급
	console.log(str.substring (0,20)); // ‘abcdefghij’ 

str.toLowerCase () , str.toUpperCase ()

	console.log(ALPHABET.toLowerCase()); // ‘alphabet’
	console.log(‘alphabet’.toUpperCase()); // ‘ALPHABET’

Immutable

모든 string method는 immutable. 즉, 원본이 변하지 않음.
array method는 immutable 및 mutable 여부를 잘 기억해야 함.

profile
나만의 학습 노트

0개의 댓글