Javascript讀書筆記:字串常用方法

來源:互聯網
上載者:User

Javascript讀書筆記:字串常用方法
concat() 串連多個字串,返回合并後的字串。 1 var s1="a";2 var s2="b";3 var s3="c";4 5 console.log(s1.concat(s2,s3));//abcconcat() 方法的結果等同於:result = s1 + s2 + ... + sN。如果有不是字串的參數,則它們在串連之前將首先被轉換為字串。 數組中的concat():將參數添加為數組的元素,返回新的數組。 1 var arr = [1, 2, 3];2 console.log(arr.concat(4, 5));//[1,2,3,4,5]indexOf() 尋找子串第一次出現的索引。 1 var s="abcd";2 console.log(s.indexOf("b"));//1若沒有匹配項,返回 -1。 1 var s="abcd";2 console.log(s.indexOf("e"));//-1可以傳入第二個參數:fromIndex,表示從索引位置fromIndex開始尋找,預設從起始索引0開始尋找;若fromIndex為負,則從索引0開始尋找。 1 var s = "aba";2 console.log(s.indexOf("a", 0));//03 console.log(s.indexOf("a", 1));//24 console.log(s.indexOf("a", -1));//0該方法對大小寫敏感。 1 var s="Aba";2 console.log(s.indexOf("a"));//2lastIndexOf() 類似 indexOf() ,但從右往左尋找。 1 var s = "aba";2 console.log(s.lastIndexOf("a"));//2也可指定起始索引,預設從最大索引開始尋找。 1 var s = "aba";2 console.log(s.lastIndexOf("a",1));//0charAt() 返回指定索引位置的字元(因為Javascript中沒有字元類型,所以返回的是長度為1的字串)。 1 var s="abc";2 console.log(s.charAt(1));//b若索引越界,返回Null 字元串。 1 var s="abc";2 console.log(s.charAt(-1));//""substr() substr(fromIndex,length):從起始索引fromIndex開始截取長度length的字串。 1 var s="abc";2 console.log(s.substr(1,1));//b若length < 1,返回Null 字元串。 1 var s="abc";2 console.log(s.substr(1,-1));//""若不指定length或者length超過可截取的最大長度,則截取到結尾。 1 var s="abc";2 console.log(s.substr(1));//bc3 console.log(s.substr(1,10));//bc若起始索引為負,則從右往左開始截取(從右往左的索引從-1開始)。 1 var s="abc";2 console.log(s.substr(-1,1));//csubstring() substring(startIndex,endIndex):截取起始索引startIndex到結束索引endIndex的子字串,結果包含startIndex處的字元,不包含endIndex處的字元。 1 var s="abc";2 console.log(s.substring(1,2));//b若startIndex或者endIndex為負,則會被替換為0。 1 var s="abc";2 console.log(s.substring(-1,2));//ab若startIndex和endIndex相等,則返回Null 字元串。 若startIndex > endIndex,則執行方法時,兩個值會被交換。 1 var s="abc";2 console.log(s.substring(2,1));//b若不指定endIndex或endIndex超出最大索引,則截取到結尾。 1 var s="abc";2 console.log(s.substring(1));//bc3 console.log(s.substring(1,10));//bcslice() slice(startIndex,endIndex):截取起始索引startIndex到結束索引endIndex的子字串,結果包含startIndex處的字元,不包含endIndex處的字元。 1 var s="abc";2 console.log(s.slice(1,2));//b若startIndex或者endIndex為負,表示該索引是從右往左計算的索引(跟substring不同)。 複製代碼1 var s="abc";2 console.log(s.slice(1,2));//b3 //等價於4 console.log(s.slice(1,-1));//b5 //等價於6 console.log(s.slice(-2,-1));//b7 //等價於8 console.log(s.slice(-2,2));//b複製代碼若startIndex所表示的索引位置等於endIndex所表示的索引位置,或者startIndex所表示的索引位置在endIndex所表示的索引位置之後,則返回Null 字元串(跟substring不同)。 1 var s="abc";2 console.log(s.slice(1,1));//""3 console.log(s.slice(-1,-2));//""split() 按給定字串分割,返回分割後的多個字串組成的字串數組。 1 var s="a,bc,d";2 console.log(s.split(","));//["a", "bc", "d"] 3 4 s="a1b1c1d1";5 console.log(s.split("1"));//["a", "b", "c", "d", ""] 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.