字符串屬性和方法總結(jié)

人生就像一列開往墳?zāi)沟牧熊?,路途上會有很多站,很難有人至始至終陪你走完全程,當(dāng)陪你的人要下車時,即便不舍,也要心存感激,然后揮手告別。---sunnyhuang

字符串是js基本但是有龐大的一個體系,有時候總是和數(shù)組的方法混淆,所以筆者特地的把字符串的屬性和方法總結(jié)下,希望對大家有幫組

字面量字符串和new String()的區(qū)別

//我們知道有2中方式可以聲明字符串,但是這2種聲明有下列幾點的區(qū)別
var str1= "hcc"
var str2= new String("hcc")
console.log(str1===str2)  //false  值相等但是類型不想等
console.log(str1==str2)  //true  
//typeof判定的類型不一樣
typeof str1  //string
typeof str2  //object
//console.log值不一樣
console.log(str1)  //hcc
console.log(str2)  //String {0: "h", 1: "c", 2: "c", length: 3, [[PrimitiveValue]]: "hcc"}
console.log(str1.charAt === str2.charAt); // true

字符串的屬性

  1. length 表示一個字符串的長度

    "hccc".length  //4
    "".length  //0
    

字符串的主要方法

  1. charAt()

    str.charAt(index) 從一個字符串中返回指定的字符 index:介于0-length-1之前

    "hcc".charAt(2)  //c
    "hcc".charAt(3)  //""
    
  2. charCodeAt()

    str.charCodeAt(index) //返回值是一表示給定索引處字符的 UTF-16 代碼單元值的數(shù)字
    
    "hcc".charCodeAt(1) //99
    
  3. indexOf()

    str.indexOf(searchValue) //返回指定值第一次出現(xiàn)的索引
    
    "hfajsfj".indexOf("a")  //2
    "hfajsfj".indexOf("j")  //3
    
  4. match()

    str.match(regexp)  //返回一個Array(一個包含了整個匹配結(jié)果以及任何括號捕獲的匹配結(jié)果的 Array ) 
    
    "H3J4JLJ9JF8f55".match(/\d+/g)  //["3", "4", "9", "8", "55"]
    
  5. replace()

    str.replace(regexp/substr,newSubStr/function)   //一個部分或全部匹配由替代模式所取代的新的字符串。
    

    該方法并不改變調(diào)用它的字符串本身,而只是返回一個新的替換后的字符串。

    //1.簡單的字符串替換
    " World peace".replace("peace","hello")  //"World hello"
    //2. 通過正則替換
    "my name is hcc".replace(/m/,"xxxx")  //"xxxxy name is hcc"
    "my name is hcc".replace(/m/g,"xxxx") //"xxxxy naxxxxe is hcc"
    //3.復(fù)雜的正則和函數(shù)
    "<li>{0},{1}</li>".replace(/\{(\d+)\}/g,function(){console.log(arguments)})
    //每次遍歷都返回一個數(shù)組,包括原有值,遍歷的索引,在字符串中的位置,原字符串
    //["{0}","0",4,"<li>{0},{1}</li>"]  ["{1}","1",8,"<li>{0},{1}</li>"]
    //4.運用這個函數(shù)的值
    function str(){
      var params=Array.prototype.slice.call(arguments,0)
      var str="<li>{0},{1}</li>".replace(/\{(\d+)\}/g,function(){return params[arguments[1]]})
      console.log(str)
    }
    str(5,51) //<li>5,51</li>
    str("hello","world")  //<li>hello,world</li>
    
  6. slice()

    str.slice(beginSlice[, endSlice]) //提取字符串的一部分,并返回新的字符串 如果省略endSlice,將會一直提取到字符串結(jié)尾 (不包括endSlice)
    

    返回一個新的字符串,不會改變原有的字符串,返回的字符串長度等于endSlice-beginSlice

    var str="my name is hcc"
    str.slice(0,4) //my n
    str.slice(0)  //"my name is hcc"
    str  //"my name is hcc"
    
  7. substr()

    str.substr(start[,length]) //返回一個字符串中指定位置開始到指定字符數(shù)的字符 如果忽略length的值,就提取字符串到結(jié)尾
    

    返回一個新的字符串,不會改變原來的字符串

    var str="my name is hcc"
    str.substr(0,5)  //my na
    str.substr(1,4)  //y na
    
  8. substring()

    str.substring(indexStart,indexEnd) //返回一個從開始索引到結(jié)束索引的新字符串 如果忽略indexEnd的值,就提取到字符串結(jié)尾
    

    返回一個新的字符串,不會改變原來的字符串。返回的字符串長度等于indexEnd-indexStart

    var str="my name is hcc"
    str.substring(1,4)  //y n
    

    記憶substr()和substring()的區(qū)別:短的是長度,長的是最后的值。

  9. toLowerCase()

  10. toUpperCase()

    str.toLowerCase()  //把字符串的值裝換成小寫
    str.toUpperCase()  //把字符串的值裝換成大寫
    

    返回一個新的字符串,不會改變原來的字符串。

    var str="my name is hcc"
    str.toUpperCase() // "MY NAME IS HCC"
    
  11. trim()

    str.trim() //刪除一個字符串2邊的空格
    

    返回一個新的字符串,不會改變原來的字符串。

    var str="my name is hcc  "
    str.trim()  //"my name is hcc"
    

    ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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