打卡時(shí)間:15:00
String類型
5、字符串大小寫轉(zhuǎn)換方法
ECMAScript中設(shè)計(jì)字符串大小寫轉(zhuǎn)換的方法有4個(gè):toLowerCase( )、toLocaleLowerCase( )、toUpperCase( )和toLocaleUpperCase( )。
其中,toLowerCase()和toUpperCase()是兩個(gè)經(jīng)典的方法,借鑒自java.lang.String中的同名方法
toLocaleLowerCase( )和toLocaleUpperCase( )是針對(duì)特定地區(qū)的實(shí)現(xiàn)。
var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase());//HELLO WORLD
alert(stringValue.toUpperCase());//HELLO WORLD
alert(stringValue.toLocaleLowerCase());//hello world
alert(stringValue.toLowerCase());//hello world
6、字符串的模式匹配方法
String類型定義了幾個(gè)用于在字符串中匹配模式的方法。
◆ match() 方法只接受一個(gè)參數(shù),要么是一個(gè)正則表達(dá)式,要么是一個(gè)RegExp對(duì)象。例:
var text = "cat , bat, sat , fat ;"
var pattern= /.at/;
var matches = text.match (pattern);
alert( matches.index);//0
alert( matches[0]);//cat
alert( pattern.lastIndex);//0
◆ search( )方法唯一參數(shù)與match( )方法的參數(shù)相同:由字符串或RegExp對(duì)象指定的一個(gè)正則表達(dá)式。
search()方法返回字符串中第一個(gè)匹配項(xiàng)的索引;如果沒有找到匹配項(xiàng),則返回-1。search()方法始終是從字符串開頭向后查找,例:
var text = "cat , bat, sat , fat ;"
var pos = text.search(/at/);
alert(pos);//1
◆ replace()方法可以簡(jiǎn)化替換子字符串。這個(gè)方法接受兩個(gè)參數(shù):
第一個(gè)參數(shù)可以是一個(gè)RegExp對(duì)象或者一個(gè)字符串。第二個(gè)參數(shù)可以是一個(gè)字符或者一個(gè)函數(shù)。
如果第一個(gè)參數(shù)是字符串,那么只會(huì)替換第一個(gè)子字符串。要想替換所有子字符串,那么就是要提供一個(gè)正則表達(dá)式,要么就是制定全局(g)標(biāo)志,例:
var text = "cat , bat, sat , fat ;"
var result = text.replace( "at","ond");
alert(result);//cond,bat,sat,fat
result = text.replace(/at/g,"ond");
alert(result);//cond,bond,sond,fond
如果第二個(gè)參數(shù)是字符串,那么可以使用一些特殊的字符序列,將正則表達(dá)式操作得到的值插入到結(jié)果字符串中。

例:
var text = "cat , bat, sat , fat ;"
result = text.replace(/(.at)/g,"word($1)");
alert(result);//word(cat),word(bat),word(sat),word(fat)
◆ split()方法可以基于制定的分隔符將一個(gè)字符串分隔成多個(gè)子字符串,并將結(jié)果放在一個(gè)數(shù)組中。分隔符可以是字符串,也可以是一個(gè)RegExp對(duì)象。split()方法可以接受可選的第二個(gè)參數(shù),用于制定數(shù)組的大小,以便確保返回的數(shù)組不會(huì)超過既定大小。例:
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");
var colors2 = colorText.split("," , 2);
var colors3 = colorText.split(/[^\,]+/);
7、localeCompare()方法
這個(gè)方法比較兩個(gè)字符串,并返回下列字符串中的一個(gè):
● 如果字符串在字母表中應(yīng)該排在字符串參數(shù)之前,則返回一個(gè)負(fù)數(shù)(大多數(shù)情況下是-1)
● 如果字符串等于字符串參數(shù),則返回0;
● 如果字符串在字母表中應(yīng)該排在字符串參數(shù)之后,則返回一個(gè)正數(shù)(大多數(shù)情況下是1)
var stringValue = "yellow";
alert(stringValue.localeCompare("brick"));//1
alert(stringValue.localeCompare("yellow"));//0
alert(stringValue.localeCompare("zoo"));//-1
localeCompare( )方法比較與眾不同的地方就是實(shí)現(xiàn)所支持的地區(qū)(國(guó)家和語(yǔ)言)決定了這個(gè)方法的行為。比如美國(guó)以英語(yǔ)作為ECMAScript實(shí)現(xiàn)的標(biāo)準(zhǔn)語(yǔ)言,因此localeCompare()就是區(qū)分大小寫的,于是大寫字母在字母表中排在小寫字母前頭就成為了一項(xiàng)決定性的比較規(guī)則。
因?yàn)閘ocaleCompare( )返回的數(shù)值取決于實(shí)現(xiàn),所以最好是像下面例子所示的使用這個(gè)方法:
function determineOrder(value){
var result = stringValue.localeCompare(value);
if(result < 0){
alert("The string 'yellow' comes before the string ' " + value + " ' .");
}else if(result >0){
alert("The string 'yellow' comes after the string ' " + value + " ' .");
}else(
alert("The string 'yellow' is equal to the string ' " + value + " ' .")
)
}
determineOrder("brick");
determineOrder("yellow");
determineOrder("zoo");
8、fromCharCode()方法
String構(gòu)造函數(shù)本身還有一個(gè)靜態(tài)方法:fromCharCode()。這個(gè)方法的任務(wù)是接受一或者多個(gè)字符編碼,然后將他們轉(zhuǎn)換成一個(gè)字符串。從本質(zhì)上看,這個(gè)方法與實(shí)例方法charCodeAt()執(zhí)行的是相反的操作,例:
alert(String.fromCharCode(104,101,108,108,111));//hello
//這里傳遞的是字符串hello中每個(gè)字母的字符編碼
9、HTML方法
早期的web瀏覽器提供商察覺到使用Javascript動(dòng)態(tài)格式化HTML的需求。于是就擴(kuò)展了標(biāo)準(zhǔn),實(shí)現(xiàn)了一些專門用于簡(jiǎn)化常見HTML格式化任務(wù)的方法。下表列出了這個(gè)HTML方法。但是盡量不使用這些方法,因?yàn)樗鼈儎?chuàng)建的標(biāo)記通常無(wú)法表達(dá)語(yǔ)義。
