[자바스크립트] string 객체

1. 설명

- 문자열을 담는 객체


2. 코드

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>변경 타이틀</title>
<!--타이틀 변경(참고:https://hkand.blogspot.com/2023/01/html5.html)-->
</head>

<body>
<h3>string 객체</h3>
<hr>
<script>
let b = new String("Birth and Death");
let d = "?";
document.write("<h3>첫번째 문장 : " + b + "<br></h3>");
document.write("<h3>두번째 문장 : " + d + "<br></h3>");
document.write("1. 첫번째 문자열은? " + b.charAt(0) + "<br>");
document.write("2. 두 문장을 합치면? " + b.concat(d) + "<br>");
document.write("3. i는 몇 번째? " + b.indexOf("i") + "<br>");
document.write("4. 6~8까지 slice하면? " + b.slice(6, 9) + "<br>");
document.write("5. 6에서 3개 문자 갯수만큼 값을 리턴하면? " + b.substr(6, 3) + "<br>");
document.write("6. 대문자로 변환하면? " + b.toUpperCase() + "<br>");
document.write("7. and를 change로 바꾸면? " + b.replace("and", "change") + "<br>");
let sub = b.split(" ");
document.write("8. 빈칸 분리?<br>");
for(let i=0; i<sub.length; i++)
document.write("sub" + i + "=" + sub[i] + "<br>");
</script>
</body>
</html>

3. 참고

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String


끝.