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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章