• JavaScript 中 substr 和 substring的区别


    String.substr(N1,N2) 这个就是我们常用的从指定的位置(N1)截取指定长度(N2)的字符串;
    String.substring(N1,N2) 这个就是我们常用的从指定的位置(N1)到
    指定的位置(N2)的字符串;

    还有如下说法:
    String.substr(N1,N2)  这个就是我们常用的从指定的位置(N1)截取指定长度(N2)的字符串;
    String.substring(N1,N2) 这个就有点特别了,它是先从N1,N2里找出一个较小的值,然后从字符串的开始位置算起,截取较小值位置和较大值位置之间的字符串,截取出来的字符串的长度为较大值与较小值之间的差。


    //其中substring就可以用substr代替,结果一样
    <html>
    <head>
    <title>Exercise / Lab</title>
    <script type="text/javascript">
    function lab16() {
    var entry = prompt("Enter a string - at least 5 alpha characters in lower case ", "");
    var entryLength = entry.length;
    document.write("<h2> You entered " + entry + "</h2>");
    if(entryLength % 2 == 0) { /* Check entry length for remainder */
    document.write("<h2>Even number of characters</h2><br />");
    document.write("<h2>" + entry.toUpperCase() + "</h2>");
    }
    else {
    document.write("<h2>Odd number of characters</h2><br />");
    var midPoint = Math.floor(entryLength / 2);
    document.write("<h2>" + entry.substring(0, midPoint).toLowerCase() +
    "<font color='#ff0000'>" + entry.substr(midPoint, 1).toUpperCase() + "</font>" +
    entry.substring(midPoint + 1).toLowerCase() + "</h2>");
    }
    }
    </script>
    </head>
    <body>
    <h2>Lab 16</h2>
    <script language='javascript' type='text/javascript'>
    <!--
    lab16();
    -->
    </script>
    </body>
    </html>
  • 相关阅读:
    假期小作业1
    Python_day4
    Python_day3
    Python_day2
    12/06
    12/05
    python系统学习:第三周之简单的三级菜单
    python系统学习:第二周之字典应用
    python系统学习:第二周之字符串函数练习
    python系统学习:第二周之购物车功能
  • 原文地址:https://www.cnblogs.com/goody9807/p/988461.html
Copyright © 2020-2023  润新知