1. 程式人生 > >JavaScript 字串基本方法

JavaScript 字串基本方法

一、anchor() 方法用於建立 HTML 錨

var txt="Hello world!"
document.write(txt.anchor("myanchor"))
輸出:<a name="myanchor">Hello world!</a>
二、 big() 方法用於把字串顯示為大號字型

var str="Hello world!"
document.write(str.big())
三、 blink() 方法用於顯示閃動的字串

var str="Hello world!"
document.write(str.blink())
注:感覺沒啥軟用~~~
四、 bold() 方法用於把字串顯示為粗體

var str="Hello world!"
document.write(str.bold())
五、 charAt() 方法可返回指定位置的字元

 index必需。表示字串中某個位置的數字,即字元在字串中的下標

var str="Hello world!"
document.write(str.charAt(1))
輸出:e
六、 charCodeAt() 方法可返回指定位置的字元的 Unicode 編碼。這個返回值是 0 - 65535 之間的整數。
 方法 charCodeAt() 與 charAt() 方法執行的操作相似,只不過前者返回的是位於指定位置的字元的編碼,而後者返回的是字元子串。  
index 必需。表示字串中某個位置的數字,即字元在字串中的下標。
var str="Hello world!"
document.write(str.charCodeAt(1))
輸出:101
七、 concat() 方法用於連線兩個或多個字串
var str1="Hello "
var str2="world!"
document.write(str1.concat(str2))
輸出:Hello world!
八、 fixed() 方法用於把字串顯示為打字機字型

var str="Hello world!"
document.write(str.fixed())
九、 fontcolor() 方法用於按照指定的顏色來顯示字串
var str="Hello world!"
document.write(str.fontcolor("Red"))
十、 fontsize() 方法用於按照指定的尺寸來顯示字串

var str="Hello world!"
document.write(str.fontsize(7))
十一、 fromCharCode(num)  可接受一個指定的 Unicode 值,然後返回一個字串

 num必需。一個或多個 Unicode 值,即要建立的字串中的字元的 Unicode 編碼。

document.write(String.fromCharCode(72,69,76,76,79))
document.write("<br />")
document.write(String.fromCharCode(65,66,67))
輸出:HELLO
:ABC 十二、 indexOf() 方法可返回某個指定的字串值在字串中首次出現的位置。

 searchvalue 必需。規定需檢索的字串值。
 fromindex可選的整數引數。規定在字串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略該引數,則將從字串的首字元開始檢索。

var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
輸出 :0
:-1 :6十三、 italics() 方法用於把字串顯示為斜體

var str="Hello world!"
document.write(str.italics())
十四、 lastIndexOf(searchvalue,fromindex) 方法可返回一個指定的字串值最後出現的位置,在一個字串中的指定位置從後向前搜尋。

searchvalue 必需。規定需檢索的字串值。
fromindex 可選的整數引數。規定在字串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略該引數,則將從字串的最後一個字元處開始檢索。

註釋:lastIndexOf() 方法對大小寫敏感!
註釋:如果要檢索的字串值沒有出現,則該方法返回 -1。

var str="Hello world!"
document.write(str.lastIndexOf("Hello") + "<br />")
document.write(str.lastIndexOf("World") + "<br />")
document.write(str.lastIndexOf("world"))
輸出:0
-1 6十五、 link(url) 方法用於把字串顯示為超連結。

 url 必需。規定要連結的 URL。

var str="Free Web Tutorials!"
document.write(str.link("http://www.baidu.com"))
十六、 replace() 方法用於在字串中用一些字元替換另一些字元,或替換一個與正則表示式匹配的子串。

 var str="Visit Microsoft!"
 document.write(str.replace(/Microsoft/, "abc"))
 輸出:Visit W3School!
十七、search() 方法用於檢索字串中指定的子字串,或檢索與正則表示式相匹配的子字串。

var str="Visit W3School!"
document.write(str.search(/W3School/))
輸出:6
十八、 slice() 方法可提取字串的某個部分,並以新的字串返回被提取的部分。

 start要抽取的片斷的起始下標。如果是負數,則該引數規定的是從字串的尾部開始算起的位置。也就是說,-1 指字串的最後一個字元,-2 指倒數第二個字元,以此類推。
 end緊接著要抽取的片段的結尾的下標。若未指定此引數,則要提取的子串包括 start 到原字串結尾的字串。如果該引數是負數,那麼它規定的是從字串的尾部開始算起的位置。

var str="Hello happy world!"
document.write(str.slice(6))
輸出:happy world!
var str="Hello happy world!"
document.write(str.slice(6,11))
輸出:happy
十九、 split() 方法用於把一個字串分割成字串陣列

 separator必需。字串或正則表示式,從該引數指定的地方分割 stringObject。
 howmany可選。該引數可指定返回的陣列的最大長度。如果設定了該引數,返回的子串不會多於這個引數指定的陣列。如果沒有設定該引數,整個字串都會被分割,不考慮它的長度。

註釋:如果把空字串 ("") 用作 separator,那麼 stringObject 中的每個字元之間都會被分割。

註釋:String.split(separator,howmany) 執行的操作與 Array.join 執行的操作是相反的。

var str="How are you doing today?"

document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))
輸出:How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
"2:3:4:5".split(":")	//將返回["2", "3", "4", "5"]
"|a|b|c".split("|")	//將返回["", "a", "b", "c"]
二十、 substr(start,length) 方法可在字串中抽取從  start  下標開始的指定數目的字元

 start必需。要抽取的子串的起始下標。必須是數值。如果是負數,那麼該引數宣告從字串的尾部開始算起的位置。也就是說,-1 指字串中最後一個字元,-2 指倒數第二個字元,以此類推。
 length可選。子串中的字元數。必須是數值。如果省略了該引數,那麼返回從 stringObject 的開始位置到結尾的字串。

var str="Hello world!"
document.write(str.substr(3))
輸出:lo world!
var str="Hello world!"
document.write(str.substr(3,7))
輸出:lo worl
二十一: substring(start,stop) 方法用於提取字串中介於兩個指定下標之間的字元。

 start必需。一個非負的整數,規定要提取的子串的第一個字元在 stringObject 中的位置。
 stop可選。一個非負的整數,比要提取的子串的最後一個字元在 stringObject 中的位置多 1。
如果省略該引數,那麼返回的子串會一直到字串的結尾。

 重要事項:與 slice() 和 substr() 方法不同的是,substring() 不接受負的引數。

var str="Hello world!"
document.write(str.substring(3))
輸出:lo world!
var str="Hello world!"
document.write(str.substring(3,7))
輸出:lo w