当前位置:首页 > JavaScript > 正文内容

【JavaScript】slice()、substring()、substr()的区别

canca17年前 (2009-03-13)JavaScript424
JavaScript中String 对象的slice()、substring()、substr()方法都能提取字符串的一部分,但使用时有所区别。


stringObject.slice(startIndex,endIndex)

返回字符串 stringObject 从 startIndex 开始(包括 startIndex )到 endIndex 结束(不包括 endIndex )为止的所有字符。

1)参数 endIndex 可选,如果没有指定,则默认为字符串的长度 stringObject.length 。

var stringObject = "hello world!";
alert(stringObject.slice(3)); // lo world!
alert(stringObject.slice(3,stringObject.length)); // lo world!

【注1】字符串中第一个字符的位置是从【0】开始的,最后一个字符的位置为【stringObject.length-1】,所以slice()方法返回的字符串不包括endIndex位置的字符。

2)startIndex 、endIndex 可以是负数。如果为负,则表示从字符串尾部开始算起。即-1表示字符串最后一个字符。

var stringObject = "hello world!";
alert(stringObject.slice(-3)); // ld!
alert(stringObject.slice(-3,stringObject.length)); // ld!
alert(stringObject.slice(-3,-1)); // ld
【注2】合理运用负数可以简化代码

3)startIndex、endIndex 都是可选的,如果都不填则返回字符串 stringObject 的全部,等同于slice(0)

var stringObject = "hello world!";
alert(stringObject.slice()); // hello world!
alert(stringObject.slice(0)); // hello world!
4)如果startIndex、endIndex 相等,则返回空串

【注3】String.slice() 与 Array.slice() 相似





stringObject.substring(startIndex、endIndex)

返回字符串 stringObject 从 startIndex 开始(包括 startIndex )到 endIndex 结束(不包括 endIndex )为止的所有字符。

1)startIndex 是一个非负的整数,必须填写。endIndex 是一个非负整数,可选。如果没有,则默认为字符串的长度stringObject.length 。

var stringObject = "hello world!";
alert(stringObject.substring(3)); // lo world!
alert(stringObject.substring(3,stringObject.length)); // lo world!
alert(stringObject.substring(3,7)); // lo w,空格也算在内[l][o][ ][w]
2)如果startIndex、endIndex 相等,则返回空串。如果startIndex 比 endIndex 大,则提取子串之前,调换两个参数。即stringObject.substring(startIndex,endIndex)等同于stringObject.substring(endIndex,startIndex)

var stringObject = "hello world!";
alert(stringObject.substring(3,3)); // 空串
alert(stringObject.substring(3,7)); // lo w
alert(stringObject.substring(7,3)); // lo w
【注4】与substring()相比,slice()更灵活,可以接收负参数。





stringObject.substr(startIndex,length)

返回字符串 stringObject 从 startIndex 开始(包括 startIndex )指定数目(length)的字符字符。

1)startIndex 必须填写,可以是负数。如果为负,则表示从字符串尾部开始算起。即-1表示字符串最后一个字符。

2)参数 length 可选,如果没有指定,则默认为字符串的长度 stringObject.length 。

var stringObject = "hello world!";
alert(stringObject.substr(3)); // lo world!
alert(stringObject.substr(3,stringObject.length)); // lo world!
alert(stringObject.substr(3,4)); // lo w
3)substr()可以代替slice()和substring()来使用,从上面代码看出 stringObject.substr(3,4) 等同于stringObject.substring(3,7)

【注5】ECMAscript 没有对该方法进行标准化,因此尽量少使用该方法。

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/334.html

标签: JavaScript
分享给朋友:

“【JavaScript】slice()、substring()、substr()的区别” 的相关文章

《Dom Scripting》读书笔记

  《Dom Scripting》读书笔记 Canca         最近看了一本《Jeremy Keith: DOM Scripting, Web design with JavaScript and the DOM. Apr...

javascript 中面向对象編程 (类的继承)

// 人的基類var Person=new ( function(){  var sex;  var name;  this.getSex=function() &nbs...

javascript IE与FireFox 一些兼容写法

1>获取控件用document.getElementById,不用document.all(FF等浏览器不支持)2><button> 会被firefox解释为提交form或者刷新页面,需要写标准<button type="button">3>使用childN...

javascript 弹出式窗体详解

1>window.prompt(text, value) 簡單的基與模態窗體的對話框,(返回你輸入)   var v=window.prompt("提示","請輸入你的名字")2>window.confirm(text,mess)  模態確認框(返回"是/否...

javascript 中面向对象编程 (类的构造)

javascript 中面向对象编程 (类的构造)

不論是用java,還是c#,又或是vb,構建一個對象都很簡單,都可以采用 classobj =new classobj()的方法構造一個類,然后使用其中的屬性以及方法,其實javascript也是一樣可以實現的。    示例:建立一個js文件,定義一個對象 E...

javascript在IE和Firefox中的兼容考虑

1.document.formName.item("itemName") 问题说明:IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];Firefox下,只能使用docum...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。