基本包裝類型-String類型

1.字符方法
  • charAt():接收一個參數,即基于0的字符索引值,返回給定位置的那個字符
var str = "hello world";
console.log( str.charAt(1) );       //e
  • charCodeAt():接收的參數同上,返回給定位置的那個字符的字符編碼
var str = "hello world";
console.log( str.charCodeAt(1) );       //101
  • ES5還定義了另一個訪問個別字符的方法:(IE7及以下返回undefined
var str = "hello world";
console.log( str[1] );       //e
2.字符串操作
  • concat():類似數組的concat()方法,用于拼接字符串,返回拼接得到的新字符串,不改變原數組;但是實踐中使用“+”加號拼接更為常見也更方便,特別是在有多個字符串拼接的情況下
  • slice():返回被操作字符串的一個子字符串,接收一或兩個參數,分別表示起始位置,結束位置
var str = "hello world";
console.log( str.slice(3) );       //lo world;只有一個參數,表示從起始位置至當前字符串末尾
console.log( str.slice(3,7) );     //lo w;有2個參數時,返回的新字符串不包括結束位置的項
console.log( str.slice(4,1) );   //起始位置小于結束位置時,返回空字符串
console.log( str.slice(-3) );      //rld;參數為負數時,用長度加上該數計算;str.slice( 11+(-3) )
console.log( str.slice(3,-4) );    //lo w;同上,str.slice(3,7)
console.log(str);                  //hello world;不改變原字符串
  • substr():返回值同slice();接收一或兩個參數,表示起始位置和截取個數
var str = "hello world";
console.log( str.substr(3) );       //lo world;只有一個參數,表示從起始位置至當前字符串末尾
console.log( str.substr(3,7) );     //lo worl;第2個參數,表示截取長度
console.log( str.substr(-3) );      //rld;第一個參數為負數時,和長度相加;str.substr( 11+(-3) )
console.log( str.substr(3,-4) );    //返回空字符串;第二個參數為負數時,轉換為0,因此沒有截取長度
console.log(str);                   //不改變原字符串
  • substring():返回值和參數都和slice()相同;區別在操作方法
var str = "hello world";
console.log( str.substring(3) );       //lo world;只有一個參數,表示從起始位置至當前字符串末尾
console.log( str.substring(3,7) );     //lo w;有2個參數時,返回的新字符串不包括結束位置的項
console.log( str.substring(-3) );      //hello world;參數為負數時,都轉換為0
console.log( str.substring(3,-4) );    //hel;參數轉換為0后變成str.substring(3,0),但是substring會把較小的數作為起始位置,較大的數作為結束位置,因此相當于str.substring(0,3)
console.log(str);                      //不改變原字符串
3.位置方法

參考數組的indexOf()lastIndexOf()方法

4.trim()方法
  • 創建字符串的副本,刪除前置和后綴的空格,返回結果
var str = "   hello world   ";
console.log( str.trim() );             //"hello world"
console.log(str);                      //不改變原字符串
5.大小寫轉換
  • toLowerCase()toUpperCase()分別將字符串全部轉換為小寫、大寫
var str = "Hello World";
console.log( str.toLowerCase() );             //hello world  
console.log( str.toUpperCase() );             //HELLO WORLD
console.log(str);                             //Hello World ;不改變原字符串
  • toLocaleLowerCase()toLocaleUpperCase()同樣也是分別將字符串全部轉換為小寫、大寫,但其是針對特定地區實現
6.模式匹配方法
  • match():接收一個參數,要么是正則表達式,要么是RegExp對象
var str = "cat,bat,sat,fat";
var pattern = /.at/;
console.log( str.match(pattern) );      
//["cat", index: 0, input: "cat,bat,sat,fat"]
//該方法返回一個數組,分別保存了與整個模式匹配的字符串,索引值及每一項匹配的字符串
  • search():參數與match()相同;返回字符串中第一個匹配項的索引,如果沒有匹配項則返回-1,而且該方法總是從字符串開頭向后查找
var str = "cat,bat,sat,fat";
var pattern = /at/;
console.log( str.search(pattern) );      //1
  • replace():替換字符串的方法,接收2個參數,第一個參數可以是RegExp對象或者一個字符串,第二個參數可以是字符串或者一個函數
var str = "cat,bat,sat,fat";
console.log( str.replace("at","ond") );     //cond,bat,sat,fat;第一個參數是字符串時,只替換第一個匹配項
console.log( str.replace(/at/g,"ond") );    //cond,bond,sond,fond;使用RegExp對象全局匹配替換全部的匹配項
  • split():基于指定的分隔符講一個字符串分割成多個子字符串組成的數組;分隔符可以是字符串也可以是RegExp對象;接收可選的第二個參數,用于指定數組的長度
var str = "cat,bat,sat,fat";
console.log( str.split(",") );      //["cat", "bat", "sat", "fat"]
console.log( str.split(",",2) );    //["cat", "bat"]
7.localeCompare()
  • 比較字符串與參數在子母表中的順序
var str = "yellow";
console.log( str.localeCompare("brike") );      //1;"brike"在子母表中排在"yellow"之前
console.log( str.localeCompare("yellow") );     //0;"yellow"等于"yellow"
console.log( str.localeCompare("zoo") );        //-1;"zoo"排在"yellow"之后
8.fromCharCode()
  • String構造函數本身的一個靜態方法,接收一個或多個字符編碼,轉換為一個字符串
console.log( String.fromCharCode(104,101,108,108,111) );      //hello
9.使用數組拼接出如下字符串
 <dl class="product">
   <dt>女裝</dt>
   <dd>短款</dd>
   <dd>冬季</dd>
   <dd>春裝</dd>
 </dl>
var prod = {
        name: '女裝',
        styles: ['短款', '冬季', '春裝']
};

function getTpl(data){
    var str = '<dl class="product">\n';
    str += "<dt>" + data.name + "</dt>\n";
    for(var i=0;i<data.styles.length;i++){
         str += "<dd>" + data.styles[i] + "</dd>\n";
    }
    return str += "</dl>";
};
console.log( getTpl(prod) );
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容