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
    
    
  • 相关阅读:
    jquery实现 图片延迟加载
    JSON在PHP中的应用
    【SAS NOTES】proc corr 检验变量相关性
    【SAS NOTES】proc freq 检验两分类变量
    【sas notes】proc sgplot拟合曲线
    【sas notes】proc sgplot折线图
    【sas notes】proc sgplot
    【SAS NOTES】proc reg 单变量线性回归
    【SAS NOTES】proc sgplot散点图
    【sas notes】sas9.2安装
  • 原文地址:https://www.cnblogs.com/IanI/p/3934397.html
Copyright © 2011-2022 走看看