字符串string

  1. 創(chuàng)建方式
var  a = new String('hello world');
var  a = 'hello world';

2.方法

  • 字符方法:
    charAt() 以單字符串的形式返回給定位置的那個(gè)字符;
    charCodeAt() 以單字符串的形式返回給定位置的那個(gè)字符編碼;
    fromCharCode() 方法接受一個(gè)或多個(gè)字符編碼,然后將他們轉(zhuǎn)換成一個(gè)字符串
    例如:
 var  c = 'hello jingdong';
        // charAt()  通過下標(biāo)找元素
        console.log(c.charAt(2));//l
        console.log(c.charAt(4));//o
        console.log(c.charAt(8));//n
        //  charCodeAt()  通過下標(biāo)找字符編碼
        console.log(c.charCodeAt(4));//111
        console.log(c.charCodeAt(8));//110
       //String.fromCharCode()  通過編碼找字符
        console.log(String.fromCharCode(111,110));//on
  • 字符串操作方法
    例子:
var  arr = 'hello';
        //concat()  字符串拼接起來, 返回拼接得到的新字符串
        var brr = arr.concat('world','!')
        console.log(brr);//hello worla !
  • slice()、substr() 和 substring()。
var  brr = 'hello world';
        console.log(brr.slice(2));//從下標(biāo)2開始到最后;
        console.log(brr.slice(2,7));//下標(biāo)為2   ,到7前一個(gè)結(jié)束
        console.log(brr.slice(2,-4));//llo w
  // substring() 有負(fù)數(shù)把負(fù)數(shù)轉(zhuǎn)化為0  從大到小 0-3;
        console.log(brr.substring(3,-5))//llo w
 // substr()  下標(biāo)  個(gè)數(shù)
       console.log(brr.substr(1,4));//  ello  下標(biāo)為1  找4個(gè)
        console.log(brr.substr(2,5));//llo w
        console.log(brr.substr(3,-4));//''空字符串
  • 字符串位置方法
 // indexOf()  通過元素找下標(biāo)    從前往后找
   var  crr = 'abcdefdbhi';
   console.log(crr.indexOf('b'));//1
   console.log(crr.indexOf('b' ,2));//7
   console.log(crr.indexOf('b' ,-5));//1
//lastIndexOf()  從后往前找
   console.log(crr.lastIndexOf('d',3));//3
   console.log(crr.lastIndexOf('d',5));//3
  • 字符串 轉(zhuǎn)數(shù)組 split()
//
 var str = 'hello';
   console.log(str.split(''));//["h", "e", "l", "l", "o"]

var a = "one,two,three,four";
   console.log(a)
   var  b = a.split(",");
   console.log(b)
   var b = a.split(",",3);//長度3
   console.log(b);// ["one", "two", "three"]
   var see = a.split('e');
   console.log(see);//["on", ",two,thr", "", ",four"]
  • trim() 去掉前面和后面的空格.
var drr = '   love   ';
  console.log(drr.trim());//love
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 創(chuàng)建方式 String類型是字符串的對(duì)象包裝類型,可以使用String構(gòu)造函數(shù)創(chuàng)建。 例 字符方法 charAt(...
    efb44152b9c8閱讀 316評(píng)論 0 0
  • length屬性 length屬性就是指字符串的長度只可讀,不可寫 charAt方法 作用 通過索引值(index...
    素彌閱讀 649評(píng)論 0 2
  • 創(chuàng)建一個(gè)函數(shù)字符串: 方法:toLocaleString()、toString和valueof()方法,都返回對(duì)象...
    李楊_web閱讀 205評(píng)論 0 0
  • ----- 最后更新【2021-10-12】----- PHP強(qiáng)化系列--目錄[https://www.jians...
    四月不見閱讀 905評(píng)論 0 29
  • 來自: http://www.cnblogs.com/freeabyss/archive/2013/05/15/3...
    樂魚先生閱讀 380評(píng)論 0 0

友情鏈接更多精彩內(nèi)容