5.4 引用類型之 string

1.字符方法

charAt() 和charCodeAt() ,這兩個方法都接收一個參數,即基於0的字符位置。其中charAt()是以單字符字符串形式返回給定位置的那個字符。charCodeAt()是返回字符編碼。

var str = 'tyt';
console.log(str.charAt(1));//y 
console.log(str.charCodeAt(1));//121

ES5中可以使用[ ]加數字索引來訪問特定字符,但在IE7中會返回undefined。

2.字符串操作方法

concat() 用於將一或多個字符串拼接起來,返回拼接得到的新的字符串;

var str = 'tyt';
var result = str.concat("小屠");
console.log(result);//tyt小屠
console.log(str);//tyt

ES5還提供了三個基於子字符串創建新字符串的方法: slice() substr() substring().這三個方法都返回被操作字符串的一個子字符串,接收一個或兩個參數。第一個參數指定子字符串的
開始位置,第二個參數(在指定情況下)表示子字符串到哪裏結束,slice() substr()的第二個參數指定的是子字符串最後一個字符後面的而爲之。而substr() 第二個參數指定的是返回的字符個數,如果沒有指定第二個參數,值將字符串長度作爲結束位置。默認返回字符串,對原始字符串沒影響。

var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl"
3.字符串位置方法

indexOf() lastIndexOf() ;他們都是從一個字符串中搜索給定的子字符串,返回子字符串的位置,麼有找到返回-1,indexOf從前向後搜索,lastIndexOf從後向前搜索。

var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while(pos > -1){
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
alert(positions); //"3,24,32,35,52"
4. trim () 方法:

這個方法會創建一個字符串的副本,刪除前置和後綴的所有空格。

var str =  '   tytabc  ';
var result = str.trim();
console.log(result);// ”tytabc‘’

Firefox 3.5+、 Safari 5+和 Chrome 8+還支持非標準的 trimLeft()和 trimRight()方法,分別用於刪除字符串開頭和末尾的空格

5.字符串大小寫轉換方法:

toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toUpperCase()

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類型定義了幾個用於在字符串模式中匹配的方法。
第一個是match() 方法,在字符串上調用這個方法,本質上與調用RegExp的exec() 方法相同,match() 方法只接受一個參數,要麼是一個正則表達式,要麼是一個RegExp對象

var text = "cat, bat, sat, fat";
var pattern = /.at/;
//與 pattern.exec(text)相同
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0

本例中的 match()方法返回了一個數組;如果是調用 RegExp 對象的 exec()方法並傳遞本例中的
字符串作爲參數,那麼也會得到與此相同的數組:數組的第一項是與整個模式匹配的字符串,之後的每
一項(如果有)保存着與正則表達式中的捕獲組匹配的字符串
還有一個search() replace() 方法。

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
alert(pos); //1

要想替
換所有子字符串,唯一的辦法就是提供一個正則表達式,而且要指定全局(g)標誌,如下所示。

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"

最後一個與模式匹配有關的方法是 split(),這個方法可以基於指定的分隔符將一個字符串分割成多個子字符串,並將結果放在一個數組中。分隔符可以是字符串,也可以是一個 RegExp 對象(這個方法不會將字符串看成正則表達式)。 split()方法可以接受可選的第二個參數,用於指定數組的大小,以便確保返回的數組不會超過既定大小。

var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2); //["red", "blue"]
var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]

7.fromCharCode() 靜態方法

接收一或多個字符編碼,將他們轉換成一個字符串;

alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"

8.Global 對象

URL編碼方法:
encodeURL() 和 encodeURLComponent() 可以對URL進行編碼,以便發送給瀏覽器,有限的URL裏不能包含某些字符;
encodeURI()主要用於整個 URI(例如, http://www.wrox.com/illegal value.htm),而 encodeURIComponent()主要用於對 URI 中的某一段(例如前面 URI 中的 illegal value.htm)進行編碼。它們的主要區別在於, encodeURI()不會對本身屬於 URI 的特殊字符進行編碼,例如冒號、正斜槓、問號和井字號;而 encodeURIComponent()則會對它發現的任何非標準字符進行編碼。來看下面的例子。

var uri = "http://www.wrox.com/illegal value.htm#start";
//"http://www.wrox.com/illegal%20value.htm#start"
alert(encodeURI(uri));
//"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"
alert(encodeURIComponent(uri));

一 般 來 說 , 我 們 使 用 encodeURIComponent() 方 法 的 時 候 要 比 使 用encodeURI()更多,因爲在實踐中更常見的是對查詢字符串參數而不是對基礎 URI進行編碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章