zoukankan      html  css  js  c++  java
  • javascript---函数substring(position1,position2),slice(position1,position2),substr(position1,length)

    重要事项:与 slice() 和 substr() 方法不同的是,substring() 不接受负的参数。

     

    substring(position1,position2) 方法用于提取字符串中介于两个指定下标之间的字符。包括position1的字符,不包括position2的字符(position从0开始)

    例子一:

    <script type="text/javascript">
    
    var str="Hello world!"
    document.write(str.substring(3))
    
    </script>
    lo world!

    例子二:
    <script type="text/javascript">
    
    var str="Hello world!"
    document.write(str.substring(3,7))
    
    </script>

    lo w

    重要事项:String 对象的方法 slice()、substring() 和 substr() (不建议使用)都可返回字符串的指定部分。slice() 比 substring() 要灵活一些,因为它允许使用负数作为参数。slice() 与 substr() 有所不同,因为它用两个字符的位置来指定子串,而 substr() 则用字符位置和长度来指定子串。

    slice(position1,position2) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。(空格也是字符)包括position1位置的字符,不包括position2位置的字符。

    例子一:
    <script type="text/javascript">
    
    var str="Hello happy world!"
    document.write(str.slice(6))
    
    </script>

    happy world!

    例子二:
    <script type="text/javascript">
    
    var str="Hello happy world!"
    document.write(str.slice(6,11))
    
    </script>

    happy

    注释:substr() 的参数指定的是子串的开始位置和长度,因此它可以替代 substring() 和 slice() 来使用。

    重要事项:ECMAscript 没有对该方法进行标准化,因此反对使用它。

    重要事项:在 IE 4 中,参数 start 的值无效。在这个 BUG 中,start 规定的是第 0 个字符的位置。在之后的版本中,此 BUG 已被修正。

    substr(position1,length) 方法可在字符串中抽取从 position1 下标开始的指定数目的字符。包括position1位置字符在内以后的length长度字符。

    例子一:
    <script type="text/javascript">
    
    var str="Hello world!"
    document.write(str.substr(3))
    
    </script>
    lo world!

    例子二:
    <script type="text/javascript">
    
    var str="Hello world!"
    document.write(str.substr(3,7))
    
    </script>
    lo worl
    
    
  • 相关阅读:
    restful风格 webapi 发送put delete请求 405 错误
    mysql 连接数据库间接性连接异常
    .net core 发布到centos The configuration file 'appsettings.json' was not found and is not optional. 找不到文件
    .net list<int>、int[]参数类型 前端调用传参方法
    long? long 可空类型转换
    EF 多对多循环引用序列化失败 解决办法
    HTML5学习之HTML标记类型
    电脑高手常用的5个按钮!(太有用了!留下了!)
    final关键字用法总结
    Java程序员面试失败的5大原因 //转自:极客网
  • 原文地址:https://www.cnblogs.com/IanI/p/3934397.html
Copyright © 2011-2022 走看看