| 实例分析JavaScript中substr和substring用法 |
| 来源:
发布时间:2008-04-10 发布人:
浏览:
人次
字体:
[大
中
小]
|
|
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>
| |
| |
|
|
|
|
| §最新评论:(评论内容只代表网友观点,与本站立场无关!) | |
|
|
|
|
| 注意:请勿在本站发布政治话题、色情及违反法律的内容。 |
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。 |