본문 바로가기

JavaScript

JavaScript String Methods(스트링 매소드)

반응형

String length

length 속성은 문자열의 길이를 반환합니다.
예시:

const str = "Hello World!";
const length = str.length; // 13

 

String slice()

slice() 메서드는 문자열의 특정 부분을 추출하여 새로운 문자열을 반환합니다. (해당 원하는 알파벳의 인덱스 위치-1을)
예시:

const str = "Hello World!";
const sliced = str.slice(6, 11); // "World"

String substring()

substring() 메서드는 주어진 인덱스 사이의 문자들을 반환합니다.
예시:

const str = "Hello World!";
const substr = str.substring(0, 5); // "Hello"

String substr()

substr() 메서드는 시작 인덱스와 길이를 기준으로 문자열을 반환합니다.
예시:

const str = "Hello World!";
const subStr = str.substr(6, 5); // "World"

 

String replace()

replace() 메서드는 문자열에서 일치하는 첫 번째 값을 새로운 값으로 대체합니다.
예시:

const str = "Hello World!";
const replaced = str.replace("World", "Everyone"); // "Hello Everyone!"

 

String replaceAll()

replaceAll() 메서드는 문자열 내의 모든 일치하는 값을 새로운 값으로 대체합니다.
예시:

const str = "Hello World World!";
const replaced = str.replaceAll("World", "Everyone"); // "Hello Everyone Everyone!"

 

String toUpperCase()

toUpperCase() 메서드는 문자열을 모두 대문자로 변환합니다.
예시:

const str = "Hello World!";
const upper = str.toUpperCase(); // "HELLO WORLD!"

 

String toLowerCase()

toLowerCase() 메서드는 문자열을 모두 소문자로 변환합니다.
예시:

const str = "Hello World!";
const lower = str.toLowerCase(); // "hello world!"

 

String concat()

concat() 메서드는 두 개 이상의 문자열을 합칩니다.
예시:

const str1 = "Hello";
const str2 = " World!";
const combined = str1.concat(str2); // "Hello World!"

 

String trim()

trim() 메서드는 문자열 양쪽 끝의 공백을 제거합니다.
예시:

const str = "   Hello World!   ";
const trimmed = str.trim(); // "Hello World!"

 

String trimStart()

trimStart() 메서드는 문자열 왼쪽 끝의 공백을 제거합니다.
예시:

const str = "   Hello World!";
const trimmed = str.trimStart(); // "Hello World!"

 

String trimEnd()

trimEnd() 메서드는 문자열 오른쪽 끝의 공백을 제거합니다.
예시:

const str = "Hello World!   ";
const trimmed = str.trimEnd(); // "Hello World!"

 

String padStart()

padStart() 메서드는 문자열 왼쪽을 특정 문자로 채워 주어진 길이를 만듭니다.
예시:

const str = "5";
const padded = str.padStart(3, '0'); // "005"

 

String padEnd()

padEnd() 메서드는 문자열 오른쪽을 특정 문자로 채워 주어진 길이를 만듭니다.
예시:

const str = "Apple";
const padded = str.padEnd(10, 'X'); // "AppleXXXXX"

 

String charAt()

charAt() 메서드는 특정 인덱스에 위치한 문자를 반환합니다.
예시:

const str = "Hello World!";
const character = str.charAt(4); // "o"

 

String charCodeAt()

charCodeAt() 메서드는 특정 인덱스에 위치한 문자의 유니코드를 반환합니다.
예시:

const str = "Hello World!";
const code = str.charCodeAt(4); // 111

 

String split()

split() 메서드는 문자열을 배열로 나눕니다. 구분자를 인자로 전달할 수 있습니다.
예시:

const str = "Hello World!";
const array = str.split(" "); // ["Hello", "World!"]
반응형

'JavaScript' 카테고리의 다른 글

javascript 날짜 date 디테일 기본 개념 잡기  (0) 2023.08.21
JavaScript 배열 디테일 잡기  (0) 2023.08.20
JavaScript 이벤트(Event)  (0) 2023.08.18
JavaScript 표시 기능 사용  (0) 2023.08.17
DOM(document)  (0) 2023.08.17