亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript 字符串(String) 對象

JavaScript?字符串(String)?對象

String 對象用于處理已有的字符塊。

如何使用長度屬性來計算字符串的長度:

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

如何為字符串添加樣式:

<html>
<meta charset="utf-8">
<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>")
</script>
</body>
</html>

如何使用 indexOf() 來定位字符串中某一個指定的字符首次出現(xiàn)的位置:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script>
</body>
</html>

如何使用 match() 來查找字符串中特定的字符,并且如果找到的話,則返回這個字符:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))
</script>
</body>
</html>

如何使用 replace() 方法在字符串中用某些字符替換另一些字符:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/,"PHP中文網(wǎng)"))
</script>
</body>
</html>

字符串對象

字符串對象用于處理已有的字符塊。

例子:

下面的例子使用字符串對象的長度屬性來計算字符串的長度。

var txt="Hello world!"
document.write(txt.length)

上面的代碼輸出為:

12

下面的例子使用字符串對象的toUpperCase()方法將字符串轉(zhuǎn)換為大寫:

var txt="Hello world!"
document.write(txt.toUpperCase())

上面的代碼輸出為:

HELLO WORLD!

字符串使用strong>split()函數(shù)轉(zhuǎn)為數(shù)組:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p id="demo">單擊按鈕顯示數(shù)組。</p>
<button onclick="myFunction()">點我</button>
<script>
function myFunction(){
var str="a,b,c,d,e,f";
var n=str.split(",");
document.getElementById("demo").innerHTML=n[0];
}
</script>
</body>
</html>

特殊字符

Javascript 中可以使用反斜線()插入特殊符號,如:撇號,引號等其他特殊符號。

查看如下 JavaScript 代碼:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);

在JavaScript中,字符串的開始和停止使用單引號或雙引號。這意味著,上面的字符串將被切成: We are the so-called

解決以上的問題可以使用反斜線來轉(zhuǎn)義引號:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);

JavaScript將輸出正確的文本字符串:We are the so-called "Vikings" from the north.

下表列出其他特殊字符,可以使用反斜線轉(zhuǎn)義特殊字符:

代碼 ??輸出

' ? ?單引號 ? ?

" ? ?雙引號 ? ?

\ ? ?斜桿 ? ?

n ? ?換行 ? ?

r ? ?回車 ? ?

t ? ?tab ? ?

b ? ?空格 ? ?

f ? ?換頁 ? ?


字符串屬性和方法

屬性:

length

prototype

constructor

方法:

charAt()

charCodeAt()

concat()

fromCharCode()

indexOf()

lastIndexOf()

match()

replace()

search()

slice()

split()

substr()

substring()

toLowerCase()

toUpperCase()

valueOf()


繼續(xù)學(xué)習(xí)
||
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) </script> </body> </html>
提交重置代碼