String.prototype.substr()
substr() 메서드는 문자열에서 특정 위치에서 시작하여 특정 문자 수 만큼의 문자들을 반환합니다
1 2 3 4 5 6 7 8 | var str = 'Mozilla'; console.log(str.substr(0, 2)); // expected output: "Mo" console.log(str.substr(2)); // expected output: "zilla" | cs |
str.indexOf(searchValue)
· arguments: 찾고자하는 문자열
· return value: 처음으로 일치하는 index, 찾고자하는 문자열이 없으면 -1
· lastIndexOf는 문자열 뒤에서부터 찾음 (뒤에서 첫번째로 일치하는 문자열)
1 2 3 4 5 6 | 'Blue Whale'.indexOf('Blue'); // 0 'Blue Whale'.indexOf('blue'); // -1 'Blue Whale'.indexOf('Whale'); // 5 'Blue Whale Whale'.indexOf('Whale'); // 5 'canal'.lastIndexOf('a'); // 3 | cs |
· see more: str.includes(searchValue)
- Internet Explorer와 같은 구형 브라우저에서는 작동하지 않으므로 주의
String.prototype.match()
특정 문자를 문자열에서 알아보는데는 match()함수를 쓰면 간단하다. (정규표현식을 사용한다)
예제1
1 2 3 4 | var str = "산토끼 토끼야 어디를 가느냐 깡총깡총 뛰면서 어디를 가느냐"; str.match(/냐/gi); // ["냐"],["냐"] str.match(/냐/gi).length; // 2 | cs |
flag
· g : 완전일치(발생할 모든 pattern에 대한 전역 검색)
· i : 대/소문자 무시
· gi : 대/소문자 무시하고 완전 일치
· 생략할 경우 기본값은 g
예제2
1 2 3 4 5 6 7 8 9 10 11 12 13 | var test = 'love you. love me. love everything!' var regExp = /love/gi; test2 = test.match(regExp); /* 위 코드는 정규표현식을 사용하여 텍스트에 love가 있는지 확인하는 방법입니다. 보시는 것처럼 정규표현식 코드는 따로 regExp 변수에 저장하였으며 표현식 뒤에 있는 gi는 대소문자 구분을 허용하지 않고 모든 패턴을 검색하기 위함입니다. 이를 실행하면 그 결과는 어떻게 될까요? 위 코드는 아래와 같은 결과값을 반환합니다. */ ['love', 'love', 'love'] // test2변수에 배열로 모든 love 텍스트가 저장됨 | cs |
str.substring(start, end)
· arguments: 시작 index, 끝 index (미포함)
· return value: 시작과 끝 index 사이의 문자열
1 2 3 4 5 6 | 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 범위를 넘을 경우 마지막 index로 취급 | cs |
· see more: str.slice(start, end)
substring과 비슷하나, 몇가지 차이점을 보임
str.toLowerCase() / str.toUpperCase() IMMUTABLE
· arguments: 없음
· return value: 대,소문자로 변환된 문자열
1 2 | console.log('ALPHABET'.toLowerCase()); // 'alphabet' console.log('alphabet'.toUpperCase()); // 'ALPHABET' | cs |
String.prototype.charAt()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // 아래 예제는 문자열 "Brave new world"의 다른 위치에 있는 문자들을 출력합니다. var anyString = 'Brave new world'; console.log("The character at index 0 is '" + anyString.charAt() + "'"); // No index was provided, used 0 as default console.log("The character at index 0 is '" + anyString.charAt(0) + "'"); console.log("The character at index 1 is '" + anyString.charAt(1) + "'"); console.log("The character at index 2 is '" + anyString.charAt(2) + "'"); console.log("The character at index 3 is '" + anyString.charAt(3) + "'"); console.log("The character at index 4 is '" + anyString.charAt(4) + "'"); console.log("The character at index 999 is '" + anyString.charAt(999) + "'"); /* 프로그램의 실행 결과는 아래와 같습니다. The character at index 0 is 'B' The character at index 1 is 'r' The character at index 2 is 'a' The character at index 3 is 'v' The character at index 4 is 'e' The character at index 999 is '' */ | cs |
String.prototype.trim()
trim() 메서드는 문자열 양 끝의 공백을 제거합니다. 공백이란 모든 공백문자(space, tab, NBSP 등)와 모든 개행문자(LF, CR 등)를 의미합니다.
return 호출 문자열 양 끝에서 공백을 제거한 새로운 문자열.
1 2 3 4 5 | var orig = ' foo '; console.log(orig.trim()); // 'foo' var orig = ' fo o '; console.log(orig.trim()); // 'fo o' | cs |