본 문서는 JavaScript 문자열(String) 객체가 제공하는 주요 메서드(match, search, slice, split, toUpperCase, toString)의 동작 원리와 활용법을 분석한 내용입니다.
<!DOCTYPE html>
<html>
<head>
<title>예제9-6String 메소드2</title>
</head>
<body>
<h3>String 메소드2</h3>
<p>match : <b id="ca"></b></p>
<p>search : <b id="cca"></b></p>
<p>slice : <b id="cnct"></b></p>
<p>split : <b id="ido"></b></p>
<p>toUpperCase : <b id="lido"></b></p>
<p>toString : <b id="rpl"></b></p>
<script>
var str = "Hello, welcome, AI, Software";
var str2 = 5678;
var ca = str.match("AI"), cca = str.search("Software");
var cnct = str.slice(7,12), ido = str.split(" ");
var lido = str.toUpperCase(), rpl = str2.toString();
document.getElementById("ca").innerHTML = ca;
document.getElementById("cca").innerHTML = cca;
document.getElementById("cnct").innerHTML = cnct;
document.getElementById("ido").innerHTML = ido;
document.getElementById("lido").innerHTML = lido;
document.getElementById("rpl").innerHTML = rpl;
</script>
</body>
</html>
String 메소드2)을 정의합니다.id 값(ca, cca, cnct, ido, lido, rpl)이 사용되었습니다.| 메서드명 | 사용 예시 | 동작 설명 및 결과 |
|---|---|---|
match(pattern) |
str.match("AI") |
지정한 정규식 또는 문자열과 일치하는 항목을 검색하여 배열로 반환합니다. ➔ AI |
search(pattern) |
str.search("Software") |
지정한 정규식 또는 문자열이 처음으로 시작되는 인덱스 위치를 반환합니다. ➔ 20 |
slice(start, end) |
str.slice(7, 12) |
인덱스 7부터 12 직전(11)까지의 문자열을 부분 추출합니다. ➔ welco |
split(separator) |
str.split(" ") |
구분자(공백 " ")를 기준으로 문자열을 분할하여 배열을 생성합니다. (DOM 출력 시 콤마로 연결됨) ➔ Hello,,welcome,,AI,,Software |
toUpperCase() |
str.toUpperCase() |
문자열 내부의 모든 영문 소문자를 대문자로 변환합니다. ➔ HELLO, WELCOME, AI, SOFTWARE |
toString() |
str2.toString() |
숫자 객체/원시 값(5678)을 문자열 형태("5678")로 변환합니다. ➔ 5678 |
String 메소드2
match : AI
search : 20
slice : welco
split : Hello,,welcome,,AI,,Software
toUpperCase : HELLO, WELCOME, AI, SOFTWARE
toString : 5678