JavaScript String
String 對象用于處理已有的字符塊。
JavaScript 字符串
一個字符串用于存儲一系列字符就像 "John Doe".
一個字符串可以使用單引號或雙引號:
實例
var carname='Volvo XC60';
你使用位置(索引)可以訪問字符串中任何的字符:
實例
字符串的索引從零開始, 所以字符串第一字符為 [0],第二個字符為 [1], 等等。
你可以在字符串中使用引號,如下實例:
實例
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
或者你可以在字符串中使用轉(zhuǎn)義字符使用引號:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <script> var carname1="Volvo XC60"; var carname2='Volvo XC60'; var answer1="It's alright"; var answer2="He is called 'Johnny'"; var answer3='He is called "Johnny"'; document.write(carname1 + "<br>") document.write(carname2 + "<br>") document.write(answer1 + "<br>") document.write(answer2 + "<br>") document.write(answer3 + "<br>") </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
字符串(String)
字符串(String)使用長度屬性length來計算字符串的長度:
實例
<!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() 來定位字符串中某一個指定的字符首次出現(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()">點我</button> <script> function myFunction(){ var str=document.getElementById("p1").innerHTML; var n=str.indexOf("locate"); document.getElementById("p2").innerHTML=n+1; } </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
如果沒找到對應(yīng)的字符函數(shù)返回-1
lastIndexOf() 方法在字符串末尾開始查找字符串出現(xiàn)的位置。
內(nèi)容匹配
match()函數(shù)用來查找字符串中特定的字符,并且如果找到的話,則返回這個字符。
實例
<!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>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
替換內(nèi)容
replace() 方法在字符串中用某些字符替換另一些字符。
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>替換 "Microsoft" 為 "php.cn" :</p> <button onclick="myFunction()">點我</button> <p id="demo">請訪問 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)換
字符串大小寫轉(zhuǎn)換使用函數(shù) toUpperCase() / toLowerCase():
實例
<!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.toUpperCase() + "</p>"); document.write("<p>" + txt.toLowerCase() + "</p>"); document.write("<p>" + txt + "</p>"); </script> <p>該方法返回一個新的字符串,源字符串沒有被改變。</p> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
字符串轉(zhuǎn)為數(shù)組
字符串使用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 代碼:
document.write(txt);
在JavaScript中,字符串的開始和停止使用單引號或雙引號。這意味著,上面的字符串將被切成: We are the so-called
解決以上的問題可以使用反斜線來轉(zhuǎn)義引號:
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()