• 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
    
    
  • 相关阅读:
    Percona 工具包 pt-online-schema-change 简介
    MySQL 中NULL和空值的区别
    MySQL二进制日志文件过期天数设置说明
    MySQL大小写敏感说明
    SpringBoot 配置Druid:不显示SQL监控 —(*) property for user to setup
    IDEA 启用/禁用 Run Dashboard
    java.lang.IllegalAccessException: Class XX can not access a member of class XXX with modifiers "private static"
    Swagger2常用注解说明
    更改IDEA默认使用JDK1.5编译项目
    Spring Boot : Swagger 2
  • 原文地址:https://www.cnblogs.com/IanI/p/3934397.html
Copyright © 2020-2023  润新知