標籤:
(3)String類型
String類型是字串封裝類型,可以使用String建構函式來建立。
var stringObject = new string("hello world");
String對象的方法也可以在所有基本的字串值中訪問到。其中,繼承的valueOf()、toLocalString()、toString()方法,都返回對象所表示的基底字元串值。
String類型的每個執行個體都有一個length屬性,表示字串中包含多個字元。
1》字元方法
- charAt()方法:接受一個參數,基於0的字元位置。以單字串的形式返回給定位置的那個字元。
var stringValue = "hello world";alert(stringValue.charAt(1)); //"e"
- charCodeAt()方法:接受一個參數,基於0的字元位置。返回給定位置的那個字元編碼。
var stringValue = "hello world";alert(stringValue.charAt(1)); //101
- ECMAScript訪問個別字元的方法。在支援該方法的瀏覽器(IE8+和其他)中,可以使用方括弧加數字索引來訪問字串中的特定字元。在IE7及更早的版本中使用該文法,會返回undefined值。
var stringValue = "hello world";alert(stringValue[1]); //"e"
2》字串操作方法
- slice()、substr()和substring()。這三個方法都會返回被操作字串的一個子字串,而且也都接受一或兩個參數。第一個參數指定子字串的開始位置,第二個參數表示子字串到哪裡結束。具體來說,slice()和substring()的第二個參數指定的是子字串最後一個字元後面的位置。而substr()的第二個參數指定的則是返回的字元個數。如果沒有給這些方法傳遞第二個參數,則將字串的長度作為結束位置。與concat()方法一樣,slice()、substr()和substring()也不會修改字串本身的值,只是返回一個基本類型的字串值。
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"
在傳遞給這些方法的參數是負值的情況下。slice()方法會將傳入的負值與字串的長度相加,substr()方法將負的第一參數加上字串的長度,而將負的第二個參數轉換為0.最後,substring()方法會把所有負值參數都轉換為0.
var stringValue = "hello world";alert(stringValue.slice(-3)); //"rld"alert(stringValue.substring(-3)); //"hello world"alert(stringValue.substr(-3)); //"lrld"alert(stringValue.slice(3,-4)); //"lo w"alert(stringValue.substring(3,-4)); //"hel"alert(stringValue.substr(3,-4)); //" "
IE的JavaScript實現在處理向substr()傳遞負值的情況時存在問題,它會返回原始的字串。IE9修複了這個問題。
3》字串位置方法
- indexOf()方法:從一個字串的開頭向後搜尋給定的子字串,然後返回子字串的位置,如果沒有找到,則返回-1。
- lastIndexOf()方法:從一個字串的末尾向前搜尋給定的子字串,然後返回子字串的位置,如果沒有找到,則返回-1。
var stringValue = "hello world";alert(stringValue.indexOf("o")); //4alert(stringValue.lastIndexOf("o")); //7
這兩個方法都可以接收可選的第二個參數,表示從字串中的哪個位置開始搜尋。
var stringValue = "hello world";alert(stringValue.indexOf("o",6)); //7alert(stringValue.lastIndexOf("o",6)); //4
在使用第二個參數的情況下,可以通過迴圈調用indexOf()或lastIndexOf()方法來找到所有匹配的子字串。
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()方法
trim()方法會建立一個字串的副本,刪除前置及尾碼的所有空格,然後返回結果。
var stringValue = " hello world ";var trimmedStringValue = stringValue.trim();alert(stringValue); //" hello world "alert(trimmedStringValue); //"hello world"
支援這個方法的瀏覽器有IE9+和其他。此外,除IE9+以外,其他瀏覽器還支援非標準的trimLeft()和trimRight()方法,分別用於刪除字串開頭和末尾的空格。
5》字串大小寫轉換方法
toLowerCase()、toLocalLowerCase()、toUpperCase()、toLocalUpperCase()
6》字串的模式比對方法
- match()方法:在字串上調用這個方法,本質上與調用RegExp的exec()方法相同。match()方法只接受一個參數,要麼是一個Regex,要麼是一個RegExp對象。返回一個數組。
var text = "cat,bat,sat,fat";var pattern = /.at/;//與pattern.exec(text)相同var matches = text.match(pattern);alert(matches.index); //0alert(matches[0]); //"cat"alert(matches.lastIndex); //0
- replace()方法:替換子字串。這個方法接受兩個參數,第一個參數可以是一個RegExp對象或一個字串(這個字串不會被轉換成Regex),第二個參數可以是一個字串或者一個函數。如果第一個參數是字串,那麼只會替換第一個子字串。要想替換所有子字串,唯一的辦法就是提供一個Regex,而且要指定全域(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"
如果第二個參數是字串,可以使用一些特殊的字元序列,將Regex操作得到的值插入到結果字串中。
replace()方法的第二個參數也可以是一個函數。在只有一個匹配項的情況下,會向這個函數傳遞3個參數:模式的匹配項、模式比對項在字串中的位置和原始字串。
function htmlEscape(text){return text.replace(/[<>"&]/g,function(match,pos,originalText){switch(match){case "<":return "<";case ">":return ">";case "&":return "&";case "\"":return """;}});}alert(htmlEscape("<p class = \"greeting\">Hello world!</p>"));//<p class="greeting">Hello world!</p>
- 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》localeCompare()方法
localeCompare()方法比較兩個字串,並返回下列值中的一個:
- 如果字串在字母表中應該排在字串參數之前,則返回一個負數(大多數情況下是-1,具體的值要視實現而定);
- 如果字串等於字串參數,則返回0;
- 如果字串在字母表中應該排在字串參數之後,則返回一個正數(大多數情況下是-1,具體的值要視實現而定)。
8》fromCharCode()方法
接受一個或多個字元編碼,然後將它們轉換成一個字串。
alert(String.fromCharCode(104,101,108,111)); //"hello"
JS參考型別(6)——基本封裝類型2