본문 바로가기

Javascript

String 객체 사용하기

charAt()

concat()

slice()

toUpperCase()

replace()

.split()

 

 

아래에 코드있어요!

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>String 객체 사용하기</h3><hr>
    <script>
        var a = new String("Boys and Girls");
        var b = "!!";//String 만 유일하게 new 없이 사용가능
       //문자열 - 문자(한개의 단어)
        document.writeln(a.charAt(0)+"<br>");
        //concat메소드는 가변인수로 선언 (매개변수 여러개)
        document.writeln(a.concat(b, "입니다.")+"<br>");
        document.writeln(a.concat(b));
        document.writeln(a.concat(b,"a","bb","cc")+"<br>");
       
        document.writeln(a.indexOf('s')+"<br>");
        document.writeln(a.lastIndexOf('s')+"<br>");        
        
        document.writeln(a.indexOf('and')+"<br>");
        document.writeln(a.indexOf('And')+"<br>");        
        document.writeln(a.slice(5,8)+"<br>");   //추출     
        document.writeln(a.toUpperCase()+"<br>");       
        document.writeln(a.replace("and","or")+"<br>");
        document.writeln("    simba    ".trim()+"<br>");//쓸모없는 공백 없애는 함수
 
        var sub = a.split("");//빈칸을 구분자로 분리 (구분한것별로 배열들이 만들어짐.)   
        //split은 입력한 값을 기준으로 나누어서 배열에 저장한다.   
            for (var i=0;i<sub.length;i++){
                document.writeln(sub[i]);
            }
            document.writeln("<hr>");
            document.writeln("<hr>");
        var s1 = s.slice(s.indexOf("?")+1, s.length);
        document.writeln(s1);
        document.writeln("<hr>");
 
        var s2 = s1.split("&");
        for(i=0; i<s2.length;i++){
            var s3=s2[i].split("=");
            document.writeln(s3[0]+"<br>");
        }
 
        document.writeln("<hr>");
        for(i=0; i<s2.length;i++){
            var s3=s2[i].split("=");
            document.writeln(s3[1]+"<br>");
        }
         
 
    </script>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

'Javascript' 카테고리의 다른 글

HTML 태그 요소 5가지!  (0) 2020.03.11
통장  (0) 2020.03.11
Date 객체 사용하기  (0) 2020.03.11
Math 객체 만들기  (0) 2020.03.11
배열 Array(예시 평균 온도 구하기, 배열 다음 for문)  (0) 2020.03.11