Javascript

来源:互联网 发布:星际争霸2 剧情知乎 编辑:程序博客网 时间:2024/06/05 16:46

String 对象的方法 slice()、substring() 和 substr() (不建议使用,ECMAScript不支持,IE4不支持)都可返回字符串的指定部分。

slice() 比 substring() 要灵活一些,因为它允许使用负数作为参数。

slice() 与 substr() 有所不同,因为它用两个字符的位置来指定子串,而 substr() 则用字符位置和长度来指定子串。

slice() 和 substring()都是包含起始索引位置-从0开始,到结束位置为止(不包含结束索引位置)


<html>
<head>
  <title></title>
</head>
<body>
<script type="text/javascript">
  var str = "Hello world!";
  document.write(str.substring(1,7) + "<br>");
  document.write(str.substr(1,7)+"<br>");
  document.write(str.slice(1,7));
</script>
</body>
</html>


ello w
ello wo
ello w

原创粉丝点击