JavaScript 字符串(String)對(duì)象
定義字符串(String)對(duì)象
JavaScript String 對(duì)象用于處理文本字符串。創(chuàng)建 String 對(duì)象語法如下:
<script language="JavaScript"> var str_object = new String( str ); var str1 = String( str ); var str2 = str; </script>
以上三種方法中,只有第一種是使用 String 構(gòu)造函數(shù)嚴(yán)格的定義一個(gè)字符串對(duì)象,返回的也是一個(gè)對(duì)象(object)。第二種是調(diào)用 String 函數(shù),將轉(zhuǎn)換參數(shù) str 為原始字符串字符串并返回。第三種是定義一個(gè)字符串變量,但在 JavaScript 仍然按照字符串對(duì)象來處理。
運(yùn)行下面的語句便可知道它們的區(qū)別:
alert( typeof str_object ); // 輸出 object
alert( typeof str1 ); // 輸出 string
alert( typeof str2 ); // 輸出 string
String 對(duì)象屬性
屬性 ? ? ? ? ? ? ? ?描述
constructor ? ?對(duì)創(chuàng)建該對(duì)象的函數(shù)的引用 ? ?
length ? ?字符串的長(zhǎng)度 ? ?
prototype ? ?向?qū)ο筇砑訉傩院头椒?? ?
字符串(String)使用長(zhǎng)度屬性length來計(jì)算字符串的長(zhǎng)度:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <script> var txt = "Hello World!"; document.write("<p>" + txt.length + "</p>"); var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.write("<p>" + txt.length + "</p>"); </script> </body> </html>
字符串使用 indexOf() 來定位字符串中某一個(gè)指定的字符首次出現(xiàn)的位置:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="p1">Click the button to locate where "locate" first occurs.</p> <p id="p2">0</p> <button onclick="myFunction()">點(diǎn)擊查看</button> <script> function myFunction(){ var str=document.getElementById("p1").innerHTML; var n=str.indexOf("locate"); document.getElementById("p2").innerHTML=n+1; } </script> </body> </html>
match()函數(shù)用來查找字符串中特定的字符,并且如果找到的話,則返回這個(gè)字符。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <script> var str="Hello world!"; document.write(str.match("world") + "<br>"); document.write(str.match("World") + "<br>"); document.write(str.match("world!")); </script> </body> </html>
replace()?方法在字符串中用某些字符替換另一些字符。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo">請(qǐng)?jiān)L問 Microsoft!</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace("Microsoft","php.cn"); document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
字符串大小寫轉(zhuǎn)換使用函數(shù)?toUpperCase()?/?toLowerCase():
var txt="Hello World!";?????? // String
var txt1=txt.toUpperCase();?? // txt1 文本會(huì)轉(zhuǎn)換為大寫
var txt2=txt.toLowerCase();?? // txt2 文本會(huì)轉(zhuǎn)換為小寫
字符串使用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"></p> <button onclick="myFunction()">點(diǎn)擊顯示</button> <script> function myFunction(){ var str="a,b,c,d,e,f"; var n=str.split(","); document.getElementById("demo").innerHTML=n[2]; } </script> </body> </html>
特殊字符
Javascript 中可以使用反斜線()插入特殊符號(hào),如:撇號(hào),引號(hào)等其他特殊符號(hào)。
查看如下 JavaScript 代碼:
var txt="We are the so-called "Vikings" from the north.";
document.write(txt);
在JavaScript中,字符串的開始和停止使用單引號(hào)或雙引號(hào)。這意味著,上面的字符串將被切成: We are the so-called
解決以上的問題可以使用反斜線來轉(zhuǎn)義引號(hào):
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)義特殊字符:
代碼 ? ?輸出
' ? ?單引號(hào) ? ?
" ? ?雙引號(hào) ? ?
\ ? ?斜桿 ? ?
n ? ?換行 ? ?
r ? ?回車 ? ?
t ? ?tab ? ?
b ? ?空格 ? ?
f ? ?換頁 ? ?