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)

你可能感兴趣的:(js 字符串截取指定字符)