반응형

String 객체의 주요 기능( * 정적(static)멤버)

indexOf(substr, [, start]) : 문자열 (start + 1)에서부터 부분 문자열을 검색하여 위치를 반환

lastIndexOf(substr, [, start]) : 문자열 (start + 1)에서부터 부분 문자열을 검색하여 위치를 반환

charAt(n) : n+1번째 문자를 반환

slice(start [,end]) : start+1 ~ end번째까지의 문자를 반환

(시작 인수가 경우 공백 반환. 인수가 음수인 경우 뒤에서 부터의 인덱스 번호로 인식)

substring(start [,end]) : start+1 ~ end번째까지의 문자를 반환

(시작 인수가 경우 스왑해서 추출. 인수가 음수 경우 0으로 인식)

substr(start [,cnt]) : start+1문자부터 cnt갯수의 문자를 반환

split(str [, limit]) : 문자열을 str 기준으로 분할(limit 분할할 최대 )

match(reg) : 정규표현식(reg) 문자열을 검색, 일치하는 부분문자열을 반환

replace(reg, rep) : 정규표현식(reg) 문자열을 검색하여 일치하는 부분을 rep으로 치환

search(reg) : 정규표현식(reg) 문자열을 검색하여 찾은 위치를 반환

toLowerCase() : 소문자로 변환

toUpperCase() : 대문자로 변환

anchor(name) : 문자열을 링크화 한다. <a name="name">으로 적용

link(url) : 문자열을 링크화 한다.  <a href="url">으로 적용

sub() : 아래첨자

sup() : 위첨자

concat(str) : 문자열을 이어 붙인다.

length : 문자열의 길이

charCodeAt(n) : n-1번째 문자를 Latin-1코드로 변환

* fromCharCode(c1, c2, …) : Latin-1코드 c1, c2, ... 문자로 변환한다.

 

string.html 예제

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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>String객체</title>
</head>
<body>
<pre>
<script type="text/javascript">
var str1 = '뜰에 뜰에 뜰에는 닭이 있다.';
document.writeln(str1.indexOf('뜰'));
document.writeln(str1.lastIndexOf('뜰'));
document.writeln(str1.indexOf(' 뜰'3));
document.writeln(str1.lastIndexOf('에'5));
document.writeln(str1.indexOf('가든'));
 
var str2 = 'TEST프로젝트';
document.writeln(str2.charAt(4));
document.writeln(str2.slice(58));
document.writeln(str2.substring(58));
document.writeln(str2.substr(53));
document.writeln(str2.split('s'));
document.writeln(str1.split('뜰'3));
 
document.writeln('위로'.anchor('top'));
document.writeln(str2.link('https://blog.itthis.me/'));
document.writeln('10'.sub());
document.writeln('10'.sup());
 
document.writeln(str2.concat('무한상사'));
document.writeln(str2.length);
</script>
</pre>
</body>
</html>
cs

 

substring.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>String객체</title>
</head>
<body>
<pre>
<script type="text/javascript">
var str = 'TEST프로젝트';
document.writeln(str.substring(85));
document.writeln(str.slice(85));
</script>
</pre>
</body>
</html>
cs

 

substring.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>String객체</title>
</head>
<body>
<pre>
<script type="text/javascript">
var str = 'TEST프로젝트';
document.writeln(str.substring(5-2));
document.writeln(str.slice(5-2));
</script>
</pre>
</body>
</html>
cs
반응형

'교육자료 > Javascript' 카테고리의 다른 글

09. Math 내장객체  (0) 2019.05.09
08. Number 내장객체  (0) 2019.05.09
06. 자바스크립트 내장객체  (0) 2019.05.09
05. 자바스크립트 예외처리  (0) 2019.05.09
04. 자바스크립트 제어문  (0) 2019.05.09

+ Recent posts