字符串截取(创建子字符串)
- str.subString(indexA[,indexB]) 从某个索引到某个索引结束(第一个包含,最后一个不包含)。
indexB不写,意味着从indexA到最后。- str.slice(beginSlice[,endSlice]) 同上,区别在于slice可以传入负值(倒着数)。
- str.substr(start[,length]) 从start开始,截取length长度的字符串。
对于以上三个创建子串的方法来说,如果是空字符串,则无论参数是什么,仍然返回空字符串。
var str = ''; console.log(str.slice(1)); //'' console.log(str.substring(1)); //'' console.log(str.substr(1)); //''
详解
- slice()
slice(start,end)方法需要两个参数start和end,返回这个字符串中从start位置的字符到(但不包含)end位置的字符的一个子字符串;如果end为undefined或不存在,则返回从start位置到字符串结尾的所有字符。
如果start是负数,则start = max(length + start,0);如果end是负数,则end = max(length + end,0);start和end无法交换位置。var stringValue = 'hello world'; console.log(stringValue.slice()); //'hello world' console.log(stringValue.slice(2)); //'llo world' console.log(stringValue.slice(2,undefined)); //'llo world' console.log(stringValue.slice(2,-5)); //'llo ' console.log(stringValue.slice(2,-20)); //'' console.log(stringValue.slice(20)); //'' console.log(stringValue.slice(-2,2)); //'' console.log(stringValue.slice(-2,-20)); //'' console.log(stringValue.slice(-2,20)); //'ld' console.log(stringValue.slice(-20,2)); //'he' console.log(stringValue.slice(-20,-2)); //'hello wor'slice()方法涉及到Number()转型函数的隐式类型转换,当start被转换为NaN时,相当于start = 0;当end被转换为NaN时(end为undefined除外),则输出空字符串。
var stringValue = 'hello world'; console.log(stringValue.slice(NaN)); //'hello world' console.log(stringValue.slice(0,NaN)); //'' console.log(stringValue.slice(true,[3])); //'el' console.log(stringValue.slice(null,undefined)); //'hello world' console.log(stringValue.slice({})); //'hello world' console.log(stringValue.slice('2',[5])); //'llo'
- substring()
substring(start,end)方法需要两个参数start和end,返回这个字符串中从start位置的字符到(但不包含)end位置的字符的一个子字符串;如果end为undefined或不存在,则返回从start位置到字符串结尾的所有字符。
如果任一参数是NaN或负数,则被0取代;如果任一参数大于字符串长度,则被字符串长度取代;如果start 大于 end,则交换它们的值。var stringValue = 'hello world'; console.log(stringValue.substring()); //'hello world' console.log(stringValue.substring(2)); //'llo world' console.log(stringValue.substring(2,undefined)); //'llo world' console.log(stringValue.substring(20)); //'' console.log(stringValue.substring(-2,2)); //'he' console.log(stringValue.substring(NaN,2)); //'he' console.log(stringValue.substring(-2,20)); //'hello world' console.log(stringValue.substring(3,2)); //'l' console.log(stringValue.substring(3,NaN)); //'hel' console.log(stringValue.substring(-20,2)); //'he' console.log(stringValue.substring(-20,-2)); //''同样地,substring()方法也涉及到Number()转型函数的隐式类型转换。
var stringValue = 'hello world'; console.log(stringValue.substring(true,[3])); //'el' console.log(stringValue.substring(null,undefined)); //'hello world' console.log(stringValue.substring({})); //'hello world' console.log(stringValue.substring('2',[5])); //'llo'
substr()substr(start,end)方法需要两个参数start和end,end代表返回的子字符串的字符个数;该方法返回这个字符串中从start位置的字符开始的end个字符的一个子字符串;如果end为undefined或不存在,则返回从start位置到字符串结尾的所有字符。如果start是负数,则start = max(length + start,0);如果start是NaN,则相当于start = 0;如果end是负数或NaN,则end = 0,因此会返回空字符串;start和end无法交换位置。该方法不是ECMAScript标准,已经被弃用;IE8-浏览器在处理向substr()传递负值的情况时存在问题,它会返回原始的字符串。
var stringValue = 'hello world'; console.log(stringValue.substr()); // 'hello world' console.log(stringValue.substr(2)); // 'llo world' console.log(stringValue.substr(2,undefined)); // 'llo world' console.log(stringValue.substr(2,NaN)); // '' console.log(stringValue.substr(NaN,2)); // 'he' console.log(stringValue.substr(20)); // '' console.log(stringValue.substr(-2,3)); // 'ld' console.log(stringValue.substr(-2,20)); // 'ld' console.log(stringValue.substr(-20,2)); // 'he' console.log(stringValue.substr(-20,-2)); // '' console.log(stringValue.substr(2,5)); // llo w同样地,substr()方法也涉及到Number()转型函数的隐式类型转换
var stringValue = 'hello world'; console.log(stringValue.substr(true,[3])); // 'el' console.log(stringValue.substr(null,undefined)); // 'hello world' console.log(stringValue.substr({})); // 'hello world' console.log(stringValue.substr('2',[5])); // 'llo w'