javascript中数组和字符串的方法对比(2)

var stringValue = 'hello world'; console.log(stringValue.slice());//'hello world' console.log(stringValue.slice(2));//'llo world' console.log(stringValue.slice(20));//'' 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(-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'

位置

字符串和数组都拥有查找位置的两个方法:indexOf()和lastIndexOf()。位置方法和中括号[]读取方法正好相反,一个是通过项查找索引,一个是通过索引查找项

【indexOf()】

indexOf(search,start)方法接收search和start两个参数,返回search首次出现的位置,如果没有找到则返回-1

字符串中的search参数会调用String()转型函数,将该参数的非字符串值转换为字符串;而数组中的search参数则使用严格相等运算符(===)进行比较

不论是数组还是字符串,第二个参数start都会隐式调用Number()转型函数,将start非数字值(undefined除外)转换为数值;若忽略该参数或该参数为undefined、NaN时,start = 0

若start参数为负数,字符串的处理是将start=0;而数组的处理是start = max(0,start+length)

var string = 'hello world world'; console.log(string.indexOf('ld'));//9 console.log(string.indexOf('ld',undefined));//9 console.log(string.indexOf('ld',NaN));//9 console.log(string.indexOf('ld',-1));//9 console.log(string.indexOf('ld',10));//15 console.log(string.indexOf('ld',[10]));//15 console.log(string.indexOf('true',[10]));//-1 console.log(string.indexOf(false,[10]));//-1

var arr = ['a','b','c','d','e','a','b']; console.log(arr.indexOf('a',undefined));//0 console.log(arr.indexOf('a',NaN));//0 console.log(arr.indexOf('a',1));//5 console.log(arr.indexOf('a',true));//5 console.log(arr.indexOf('a',-1));//max(0,-1+7)=6; -1 console.log(arr.indexOf('a',-5));//max(0,-5+7)=2; 5 console.log(arr.indexOf('a',-50));//max(0,-50+7)=0; 0

【lastIndexOf()】

与indexOf()方法相反,lastIndexOf()方法是从右向左查找

lastIndexOf(search,start)方法接收search和start两个参数,返回searchString第一次出现的位置,如果没有找到则返回-1

类似地,字符串中的search参数会调用String()转型函数,将该参数的非字符串值转换为字符串;而数组中的search参数则使用严格相等运算符(===)进行比较

不论是数组还是字符串,第二个参数start都会隐式调用Number()转型函数,将start非数字值(undefined除外)转换为数值

若忽略该参数或该参数为undefined、NaN时,字符串的处理是start = length - 1;而数组的处理是start = 0

若start参数为负数,字符串的处理是将start=0;而数组的处理是start = max(0,start+length)

var string = 'hello world world'; console.log(string.lastIndexOf('ld'));//15 console.log(string.lastIndexOf('ld',undefined));//15 console.log(string.lastIndexOf('ld',NaN));//15 console.log(string.lastIndexOf('ld',-1));//-1 console.log(string.lastIndexOf('h',-1));//0 console.log(string.lastIndexOf('w',undefined));//12 console.log(string.lastIndexOf('ld',10));//9 console.log(string.lastIndexOf('ld',[10]));//9 console.log(string.lastIndexOf('true',[10]));//-1 console.log(string.lastIndexOf(false,[10]));//-1

var arr = [1,2,3,'1','2','3']; console.log(arr.lastIndexOf('2'));//4 console.log(arr.lastIndexOf(3));//2 console.log(arr.lastIndexOf(0));//-1 var arr = ['a','b','c','d','e','a','b']; console.log(arr.lastIndexOf('b'));//6 console.log(arr.lastIndexOf('b',undefined));//-1 console.log(arr.lastIndexOf('a',undefined));//0 console.log(arr.lastIndexOf('b',NaN));//-1 console.log(arr.lastIndexOf('b',1));//1 console.log(arr.lastIndexOf('b',-1));//max(0,-1+7)=6; 6 console.log(arr.lastIndexOf('b',-5));//max(0,-5+7)=2; 1 console.log(arr.lastIndexOf('b',-50));//max(0,-50+7)=0; -1

以上这篇javascript中数组和字符串的方法对比就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzsywd.html