国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

String 字符串對象

String 字符串對象

(1)String 的屬性

該對象只有一個屬性,即 length,表示字符串中的字符個數(shù),包括所有的空格和符號:

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

顯示結(jié)果是“11”因為字符串長度將符號和空格也計算在內(nèi):

QQ截圖20161012162429.png

訪問字符串對象的方法

使用 String 對象的 toUpperCase() 方法來將字符串小寫字母轉(zhuǎn)換為大寫:

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

以上代碼執(zhí)行后,mynum 的值是:HELLO WORLD!

(2)String 的方法

String 對象共有 19 個內(nèi)置方法,主要包括字符串在頁面中的顯示、字體大小、字體顏色、字符的搜索以及字符的大小寫轉(zhuǎn)換等功能,下面是一些常用的:

charAt(n) :返回該字符串第 n 位的單個字符。(從 0 開始計數(shù))

charCodeAt(n) :返回該字符串第 n 位的單個字符的 ASCII 碼。

indexOf() :用法:string_1.indexOf(string_2,n); 從字符串 string_1 的第 n 位開始搜索,查找 string_2,返回查找到的位置,如果未找到,則返回 -1,其中 n 可以不填,默認(rèn)從第 0 位開始查找。

lastIndexOf() :跟 indexOf() 相似,不過是從后邊開始找。

split('分隔符') :將字符串按照指定的分隔符分離開,返回一個數(shù)組,例如:'1&2&345&678'.split('&');返回數(shù)組: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.miracleart.cn") + "</p>")
</script>
</body>
</html>


繼續(xù)學(xué)習(xí)
||
<!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>
提交重置代碼