문자열 문자열 객체로그인

문자열 문자열 객체

문자열 문자열 객체

(1) 문자열의 속성

객체에는 길이를 나타내는 하나의 속성인 길이만 있습니다. 문자열 모든 공백과 기호를 포함한 문자 수:

var test_var = "I love You!";
document.write(test_var.length);

는 문자열 길이에 기호와 공백도 포함되므로 결과를 "11"로 표시합니다.

QQ截图20161012162429.png

문자열 객체에 액세스하는 방법:

문자열 객체의 toUpperCase() 메서드를 사용하여 문자열 소문자를 대문자로 변환:

var mystr="Hello world!";
var mynum=mystr.toUpperCase();

위 코드 마지막으로 mynum의 값은 다음과 같습니다. HELLO WORLD!

(2)String method

String 개체에는 총계가 있습니다. 19가지 내장 메소드 중 주로 페이지에 문자열 표시, 글꼴 크기, 글꼴 색상, 문자 검색 및 대소문자 변환과 같은 기능을 포함합니다.

charAt(n) : 문자열의 n번째 단일 문자를 반환합니다. (0부터 계산)

charCodeAt(n): 문자열의 n번째 위치에 있는 단일 문자의 ASCII 코드를 반환합니다.

indexOf() : 사용법: string_1.indexOf(string_2,n); 문자열 string_1의 n번째 위치부터 검색을 시작하고, string_2를 검색하고, 찾지 못한 경우 찾은 위치를 반환합니다. 이면 -1이 반환되고 n은 비워 둘 수 있으며 기본적으로 0번째 위치부터 검색이 시작됩니다.

lastIndexOf(): indexOf()와 비슷하지만 뒤에서 시작합니다.

split('separator') : 지정된 구분 기호에 따라 문자열을 구분하고 배열을 반환합니다. 예: '1&2&345&678'.split('&'); 1,2,345,678.

substring(n,m) : n 위치에서 m 위치까지 원래 문자열의 하위 문자열을 반환합니다.

substr(n,x) : 위치 n에서 시작하고 길이가 x인 원래 문자열의 하위 문자열을 반환합니다.

toLowerCase(): 원래 문자열의 대문자가 모두 소문자로 변경된 문자열을 반환합니다.

toUpperCase() : 원래 문자열의 모든 소문자를 대문자로 변경한 문자열을 반환합니다.

문자열 길이 계산

<html>
<body>
<script type="text/javascript">
var txt="Hello World!"
document.write(txt.length)
</script>
</body>
</html>

문자열에 스타일 추가

<html>
<body>
<script type="text/javascript">
var txt="Hello World!"
document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")
document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")
document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")
document.write("<p>Fixed: " + txt.fixed() + "</p>")
document.write("<p>Strike: " + txt.strike() + "</p>")
document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")
document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")
document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")
document.write("<p>Link: " + txt.link("http://www.php.cn") + "</p>")
</script>
</body>
</html>


다음 섹션
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>string对象 </title> <script type="text/javascript"> var message="I love JavaScript!"; var mychar=message.toLowerCase(); document.write("字符串为:"+mychar+"<br>"); </script> </head> <body> </body> </html>
코스웨어