개발/Javascript

String 메서드 모음

반응형

String.prototype.indexOf : 전달 받은 인수와 매치되는 첫번째 인덱스를 반환
                                ex) const str = 'hello world';
                                      str.indexOf('l')  // 2

                                      str.indexOf('l',3)  // 3  (두번째 인수가 있으면 그 인덱스부터 검색)  

                                      str.indexOf('z')  // -1

String.prototype.search : 전달 받은 정규 표현식과 매치되는 첫번째 인덱스를 반환
                                ex) const str = 'hello world';
                                      str.search(/o/)  // 4

                                      str.search(/z/)  // -1

 

String.prototype.includes : [ES6] 전달 받은 문자열이 포함되어 있으면 true, 아니면 false 반환
                                ex) const str = 'hello world';
                                      str.includes('hello')  // true

                                      str.includes('x')  // false

 

 String.prototype.startsWith : [ES6] 전달 받은 문자열로 시작하면 true, 아니면 false
                                ex) const str = 'hello world';
                                      str.startsWith('hell')  // true

                                      str.startsWith('world')  // false

 

 String.prototype.endsWith : [ES6] 전달 받은 문자열로 끝나면 true, 아니면 false
                                ex) const str = 'hello world';
                                      str.endsWith('hell')  // false

                                      str.endsWith('d')  // true

 

 String.prototype.charAt : 전달받은 인덱스 위치의 문자 반환
                                ex) const str = 'hello world';
                                      str.charAt(0)  // h

                                      str.charAt(7)  // o

 

 String.prototype.substring : 첫번째 인수의 인덱스 부터 두번째 인수의 인덱스 앞까지 문자열 반환
                                ex) const str = 'hello world';
                                      str.substring(1,4)  // ell

                                      str.substring(7)  // orld

 

 String.prototype.slice : substring과 동일하게 동작 단, 음수인수도 받을 수 있음(음수는 뒤에서 부터 시작)
                                ex) const str = 'hello world';
                                      str.slice(1,4)  // ell

                                      str.substring(-5)  // world

 

 String.prototype.toUpperCase : 전달받은 문자열을 모두 대문자로 반환
                                ex) const str = 'hello world';
                                      str.toUpperCase(str)  // HELLO WORLD

 

 String.prototype.toLowerCase : 전달받은 문자열을 모두 소문자로 반환
                                ex) const str = 'Hello World';
                                      str.toLowerCase(str)  // hello world

 

 String.prototype.repeat : [ES6] 전달받은 정수만큼 반복해서 새로운 문자열 반환
                                ex) const str = 'Hello World';
                                      str.repeat(3)  // Hello WorldHello WorldHello World

 

 String.prototype.replace : 첫번째 인수의 문자열을 찾아서 두번째 인수의 문자열로 치환 후 반환
                                ex) const str = 'Hello World';
                                      str.replace('World', 'Coding')  // Hello Coding

 

String.prototype.split : 전달받은 인수로 문자열을 구분해서 배열로 반환
                                ex) const str = 'hello world happy world';
                                      str.split(' ')  // ['hello','world','happy','world']

                                      str.split(' ',2)  // ['hello','world']

반응형

'개발 > Javascript' 카테고리의 다른 글

라이브러리 없이 만드는 간단한 로딩바(loadingbar)  (0) 2024.03.25
Array 배열 메서드 모음  (0) 2023.09.11
체크박스 전체 선택 코드  (0) 2023.06.18
html에서 style, script 위치  (0) 2022.01.18
데이터 타입  (0) 2021.08.28