1. 程式人生 > >js中字串的使用

js中字串的使用

javascript中String的使用 String物件 String 物件用於處理文字(字串)。字串的長度不可更改,所有的方法不能修改 內容

String物件的建立 1:var str = new String(“hello,world”);//構造器 2:var str = “hello,world”

“h_e_l_l_o_,_w_o_r_l_d” 1:big() 用大號字型顯示字串。

語法:str.big() 執行: var str=“Hello world!” document.write(str.big()) 輸出:Hello:Hello world!

2:small() 字串顯示為小號字。

語法:str.small() 執行:

var str="Hello world!"
document.write(str.small())

輸出:Hello:Hello world!

3:toLowserCase() 字串轉換為小寫。

語法:str.toLowserCase() 執行:

var str="Hello world!"
document.write(str.toLowserCase())

輸出:hello world! 4:toLocaleLowerCase() 按照本地方式把字串轉換為小寫

語法:str.toLocaleLowerCase() 執行:

var str="Hello world!"
document.write(str.toLocaleLowerCase())

輸出:hello world! 5:toUpperCase() 字串轉換為大寫

語法:str.toUpperCase() 執行:

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

輸出:HELLO WORLD 6:toLocalUpperCase() 按照本地方式把字串轉換為大寫

語法:str.toLocalUpperCase() 執行:

var str="Hello world!"
document.write(str.toLocalUpperCase())

輸出:HELLO WORLD 7:sub() 把字串顯示為下標。

語法:str.sub() 執行:

var str="Hello world!"
document.write("sub:",str.sub())

輸出:sub:Hello world!

8:sup() 把字串顯示為上標。

語法:str.sub() 執行:

var str="Hello world!"
document.write("sup:",str.sup())

輸出:sup:Hello world! 注:上下標這裡每做特殊顯示,擔待~~ 9:bold() 把字串顯示為粗體。

語法:str.bold() 執行:

var str="Hello world!"
document.write("bold:",str.bold())

輸出:bold:Hello world!

10:italics() 把字串顯示為斜體。 語法:str.italics() 執行:

var str="Hello world!"
document.write("italics:",str.italics())

輸出:italics:Hello world!

11:strike() 顯示加刪除線的字串。 語法:str.strike() 執行:

var str="Hello world!"
document.write("strike:",str.strike())

輸出:strike:Hello world!

12:blink() 用於顯示閃動的字串。 語法:str.blink() 執行:

var str="Hello world!"
document.write("blink:",str.blink())

輸出:blink:Hello world!

13:link() 用於把字串顯示為超連結。 語法:str.link() 執行:

var str="百度"
document.write(str.link("http://baidu.com"))

輸出:百度

14:anchor() 用於建立 HTML 錨。 語法:str.anchor() 執行:

var str="anchor"
document.write(str.anchor("anchor"))

輸出:anchor

15:fixed() 以列印字型顯示文字 語法:str.fixed() 執行:

var str="hello,world"
document.write(str.fixed())

輸出:hello,world 16:fontcolor() 按照指定的顏色來顯示字串。 語法:str.fontcolor(“red”) 執行:

var str="hello,world";
document.write(str.fontcolor("red"))

輸出:fontcolor:Hello world!

17:fontsize(n) 用於按照指定的尺寸來顯示字串。 1<= n <= 7 語法:str.fontsize(“red”) 執行:

var str="hello,world";
document.write(str.fontsize("7"))

輸出:fontsize:Hello world!

18:indexOf() 返回某個指定的字串值在字串中首次出現的位置。 語法:str.indexOf(value,index) value:檢索的字串 index:檢索的開始位置 indexOf() 方法對大小寫敏感! 如果要檢索的字串值沒有出現,則該方法返回 -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 19:lastIndexOf() 返回一個指定的字串值最後出現的位置,在一個字串中的指定位置從後向前搜尋。 語法:str.lastIndexOf(value,index) value:檢索的字串 index:檢索的開始位置 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 20:charAt() 返回指定位置的字元 語法:str.charAt(index) 執行:

var str = "hello,world";
document.write(str.charAt(1));

輸出:e 21:charCodeAt() 返回指定位置的字元的 Unicode 編碼。這個返回值是 0 - 65535 之間的整數。 語法:str.charCodeAt(index) 輸入:

var str = "hello,world";
document.write(str.charCodeAt(1));

輸出:101 22:fromCharCode() 可接受一個指定的 Unicode 值,然後返回一個字串。 語法:String.fromCharCode(numX,numX,…,numX) 輸入:

document.write(String.fromCharCode(72,69,76,76,79)+"<br />")
document.write(String.fromCharCode(65,66,67))

輸出:6HELLO ABC 23:match() 在字串內檢索指定的值,或找到一個或多個正則表示式的匹配。 語法: str.match(searchvalue) str.match(regexp) 存在,返回值 不存在,返回null 輸入:

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!"))
var str1="1 plus 2 equal 3"
document.write(str1.match(/\d+/g))

輸出:world null null world! 1,2,3 24:repalce() 在字串中用一些字元替換另一些字元,或替換一個與正則表示式匹配的子串。 語法:str.replace(regexp/str,str) 輸入:

var str="hello world world"
document.write(str.replace(/world/, "js"))
document.write(str.replace(/world/g, "js"))

輸出:hello js world hello js js 25:search() 用於檢索字串中指定的子字串,或檢索與正則表示式相匹配的子字串。 存在,返回索引位置 不存在,返回-1 語法:str.search(regexp) 輸入:

var str="hello world world"
document.write(str.search(/world/))

輸出:6 26:substr() 可在字串中抽取從 start 下標開始的指定數目的字元。 語法:str.substr(start.length) 輸入:

var str = "hello world";
document.write(str.substr(1,3)+"<br/>")

輸出:ell 27:substring() 提取字串中兩個指定的索引號之間的字元。 語法:str.substring(start,end) 輸入:

var str = "hello world";
document.write(str.substring(1,3)+"<br/>")

輸出:el 28:concat() 連線兩個或多個字串。 語法: var str1 = “hello”; var str2 = “world”; str.concat(str2) 輸入:

var str1 = "hello";
var str2 = "world";
document.write(str.concat(str2)+"<br/>")

輸出:hello world 29:localCompare() 用本地特定的順序來比較兩個字串。

30:slice() 提取字串的片斷,並在新的字串中返回被提取的部分。 語法: str.slice(start,end) 輸入:

var str = "hello world";
document.write(str.slice(1)+"<br/>")
document.write(str.slice(1,2)+"<br/>")

輸出:ello world e 31:split() 把一個字串分割成字串陣列。 語法: str.split(separator,size) separator:字串或正則表示式,從該引數指定的地方分割 str。 size:生成陣列長度 輸入:

var str = "hello world";
document.write(str.split("")+"<br/>")
document.write(str.split(" ")+"<br/>")
document.write(str.split("e")+"<br/>")
document.write(str.split("",3)+"<br/>")

輸出: h,e,l,l,o, ,w,o,r,l,d hello,world h,llo world h,e,l 32:toSource() 代表物件的原始碼。

33:toString() 返回一個字串。

語法:str.toString() 輸入:

var str = "hello world"
document.write(str.valueOf())

輸出: hello world 34:valueOf() 返回某個字串物件的原始值。 語法:str.valueOf() 輸入:

var str = "hello world"
document.write(str.valueOf())

輸出:hello world

注:本文中有部分特殊效果沒有展示出來~~望大家諒解!