js 字符串截取指定字符

    let str = 'hello world'
    //如截取hello
    //indexOf,查找字符串,有返回下標,沒有返回-1
    let index = str.indexOf('hello')
    //substring,參數是從哪截取到哪,不接受負數
    let cutOut1 = str.substring(index, index + 5)
    //substr,參數是從哪截取幾個
    let cutOut2 = str.substr(index, 5)
    //slice,參數是參數是從哪截取到哪,可以負數
    let cutOut3 = str.slice(index, index + 5)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章