js_str.slice()_str.substring().html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js_str.slice()_str.substring()</title>
</head>
<body>

<script>
    // 参考:https://www.runoob.com/jsref/jsref-slice-string.html
    /*1.string.slice(start,end)
    方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
    使用 start(包含) 和 end(不包含) 参数来指定字符串提取的部分。
    字符串中第一个字符位置为 0, 第二个字符位置为 1, 以此类推。
    如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。
    也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。

    start 必选,要抽取的片断的起始下标。第一个字符位置为 0
    end	可选。 要截取的片段结尾的下标。
        若未指定此参数,则要提取的子串是从 start 到原字符串结尾。*/
    let str = "Hello happy world!";
    console.log(str.slice(6));
    // happy world!
    console.log(str.slice(6,11));
    // happy
    str = "123<br>";
    console.log(str.slice(0,-4));
    // 123
    str = "津南控规";
    console.log(str.slice(-2));
    // 控规

    /*2.substring() 方法用于提取字符串中介于两个指定下标之间的字符。
    stringObject.substring(start,stop)
    start   必需。一个非负的整数
    stop     可选。一个非负的整数
    重要事项:与 slice() 方法不同的是,substring() 不接受负的参数。*/
    str = "Hello world!";
    console.log(str.substring(3));
    // lo world!
    console.log(str.substring(3).length === str.length - 3);  // true
    console.log(str);
    // Hello world!
    console.log((str.substring(3,7)));
    // lo w
    // 返回字符串的长度7-3=4。
</script>

</body>
</html>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值