|
JavaScript中substr和substring的区别: JavaScript中String 对象的slice()、substring()、substr()方法都能提取字符串的一部分,但使用时有所区别。
返回字符串 stringObject 从 startIndex 开始(包括 startIndex )到 endIndex 结束(不包括 endIndex )为止的所有字符。
var stringObject = "hello world!"; alert(stringObject.slice(3)); // lo world! alert(stringObject.slice(3,stringObject.length)); // lo world!
var stringObject = "hello world!"; alert(stringObject.slice(-3)); // ld! alert(stringObject.slice(-3,stringObject.length)); // ld! alert(stringObject.slice(-3,-1)); // ld
var stringObject = "hello world!"; alert(stringObject.slice()); // hello world! alert(stringObject.slice(0)); // hello world!
返回字符串 stringObject 从 startIndex 开始(包括 startIndex )到 endIndex 结束(不包括 endIndex )为止的所有字符。
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]
var stringObject = "hello world!"; alert(stringObject.substring(3,3)); // 空串 alert(stringObject.substring(3,7)); // lo w alert(stringObject.substring(7,3)); // lo w
返回字符串 stringObject 从 startIndex 开始(包括 startIndex )指定数目(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
【注5】ECMAscript 没有对该方法进行标准化,因此尽量少使用该方法。 |


评论表单加载中...