본문 바로가기
개발/JavaScript

[JS] 문자열 string 메서드

by WaDDak 2024. 9. 6.

1. charAt()

  • 설명: 문자열의 특정 인덱스에 위치한 문자를 반환합니다.
  • 예제: str.charAt(index)

2. charCodeAt()

  • 설명: 문자열의 특정 인덱스에 위치한 문자의 유니코드 값을 반환합니다.
  • 예제: str.charCodeAt(index)

3. concat()

  • 설명: 두 개 이상의 문자열을 연결하여 하나의 문자열로 만듭니다.
  • 예제: str1.concat(str2, ...)

4. includes()

  • 설명: 문자열에 특정 문자열이 포함되어 있는지 여부를 반환합니다.
  • 예제: str.includes(searchString)

5. indexOf()

  • 설명: 문자열에서 특정 값이 처음으로 나타나는 위치를 반환합니다. 값이 없으면 -1을 반환합니다.
  • 예제: str.indexOf(searchValue)

6. lastIndexOf()

  • 설명: 문자열에서 특정 값이 마지막으로 나타나는 위치를 반환합니다.
  • 예제: str.lastIndexOf(searchValue)

7. slice()

  • 설명: 문자열의 일부분을 추출하여 새로운 문자열을 반환합니다.
  • 예제: str.slice(beginIndex, endIndex)

8. substring()

  • 설명: 문자열의 특정 범위에 해당하는 부분 문자열을 반환합니다.
  • 예제: str.substring(beginIndex, endIndex)

9. substr()

  • 설명: 문자열의 특정 인덱스에서 지정된 길이만큼의 부분 문자열을 반환합니다.
  • 예제: str.substr(start, length)

10. toLowerCase()

  • 설명: 문자열을 소문자로 변환하여 반환합니다.
  • 예제: str.toLowerCase()

11. toUpperCase()

  • 설명: 문자열을 대문자로 변환하여 반환합니다.
  • 예제: str.toUpperCase()

12. trim()

  • 설명: 문자열 양쪽 끝의 공백을 제거하여 반환합니다.
  • 예제: str.trim()

13. replace()

  • 설명: 문자열에서 특정 부분을 다른 문자열로 교체합니다.
  • 예제: str.replace(searchValue, newValue)

14. split()

  • 설명: 문자열을 특정 구분자로 나누어 배열로 반환합니다.
  • 예제: str.split(separator)

15. repeat()

  • 설명: 문자열을 지정된 횟수만큼 반복한 새 문자열을 반환합니다.
  • 예제: str.repeat(count)

16. match()

  • 설명: 정규 표현식과 일치하는 부분을 배열로 반환합니다.
  • 예제: str.match(regexp)

17. search()

  • 설명: 정규 표현식과 일치하는 부분의 첫 번째 인덱스를 반환합니다. 일치하는 문자가 없다면 -1을 반환합니다.
  • 예제: str.search(regexp)

18. startsWith()

  • 설명: 문자열이 특정 문자로 시작하는지 여부를 반환합니다.
  • 예제: str.startsWith(searchString)

19. endsWith()

  • 설명: 문자열이 특정 문자로 끝나는지 여부를 반환합니다.
  • 예제: str.endsWith(searchString)

 

let str = "Hello World";

// 1. charAt()
console.log("1:", str.charAt(1));  // "e"

// 2. charCodeAt()
console.log("2:", str.charCodeAt(0));  // 72

// 3. concat()
console.log("3:", str.concat(" from JS"));  // "Hello World from JS"

// 4. includes()
console.log("4:", str.includes("World"));  // true

// 5. indexOf()
console.log("5:", str.indexOf("World"));  // 6

// 6. lastIndexOf()
console.log("6:", str.lastIndexOf("o"));  // 7

// 7. slice()
console.log("7:", str.slice(0, 5));  // "Hello"

// 8. substring()
console.log("8:", str.substring(0, 5));  // "Hello"

// 9. substr()
console.log("9:", str.substr(6, 5));  // "World"

// 10. toLowerCase()
console.log("10:", str.toLowerCase());  // "hello world"

// 11. toUpperCase()
console.log("11:", str.toUpperCase());  // "HELLO WORLD"

// 12. trim()
let strWithSpaces = "   Hello World   ";
console.log("12:", strWithSpaces.trim());  // "Hello World"

// 13. replace()
console.log("13:", str.replace("World", "JS"));  // "Hello JS"

// 14. split()
console.log("14:", str.split(" "));  // ["Hello", "World"]

// 15. repeat()
console.log("15:", str.repeat(2));  // "Hello WorldHello World"

// 16. match()
console.log("16:", str.match(/o/g));  // ["o", "o"]

// 17. search()
console.log("17:", str.search("World"));  // 6

// 18. startsWith()
console.log("18:", str.startsWith("Hello"));  // true

// 19. endsWith()
console.log("19:", str.endsWith("World"));  // true

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

[JS] stack  (0) 2024.09.27
[JS] Map 의 메서드 정리  (0) 2024.08.27
[JS] undefined 와 null  (0) 2024.08.20
[JS] 가비지 콜렉터(Garbage Collecto)  (0) 2024.08.20
[JS] 데이터 타입의 종류  (0) 2024.08.20