JS參考型別(6)——基本封裝類型2

來源:互聯網
上載者:User

標籤:

(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》字串操作方法
  • concat()方法:用於將一或多個字串拼接起來,返回拼接得到的新字串。可以接受任意多個參數。【使用+操作符】
    var stringValue = "hello ";var result = stringValue.concat("world","!");alert(result); //"hello world!"alert(stringValue); //"hello"
  • 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
  • search()方法:這個方法唯一的參數與match()方法的參數相同,由字串或RegExp對象指定的一個Regex。返回字串中第一個匹配項的索引。如果沒有找到匹配項,則返回-1。此方法始終是從字串開頭向後尋找模式。
    var text = "cat,bat,sat,fat";var pos = text.search(/at/);alert(pos); //1
  • 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 "&lt;";case ">":return "&gt;";case "&":return "&amp;";case "\"":return "&quot;";}});}alert(htmlEscape("<p class = \"greeting\">Hello world!</p>"));//&lt;p class=&quot;greeting&quot;&gt;Hello world!&lt;/p&gt;
  • 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. 如果字串在字母表中應該排在字串參數之前,則返回一個負數(大多數情況下是-1,具體的值要視實現而定);
  2. 如果字串等於字串參數,則返回0;
  3. 如果字串在字母表中應該排在字串參數之後,則返回一個正數(大多數情況下是-1,具體的值要視實現而定)。
8》fromCharCode()方法

接受一個或多個字元編碼,然後將它們轉換成一個字串。

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

 

JS參考型別(6)——基本封裝類型2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.