前端工程师-JavaScript-String的基本操作

说明一下:此次es6新增的一些查找字符的方法,基本都是针对大于0XFFFF编码的字符,例如:String.fromCodePoint(),为了弥补String.fromCharCode不能识别大于0xFFFF的码点而创造的。

首先先给定一个字符串:

var a = "abcd1234abcdab";

1.判断a是否是字符串

Object.prototype.toString.call(a) === "[object String]";//true

typeof a === "string"//true

2.查找:

a.charAt(index)方法可用来获取指定位置的字符,还有at()

console.log(a.charAt(2));     //返回c
console.log(str.charAt(8));     //返回空字符串

b.indexOf(str, index)指示str是否包含在字符串对象中

str:要查找的字符串/字符;

index:查找的起始位置;(可选)

返回: str所在的位置,不存在返回-1

ps:对大小写敏感

a.indexOf("b");//1
a.indexOf("f");//-1

c.lastIndexOf(str,index)返回str最后出现的位置

str:要查找的字符串/字符;

index:查找的起始位置;(可选)

  从index位置向前搜索,

  范围0 到 a.length - 1

  如果省略默认从最后一个字符处开始检索

返回: str 的第一个字符在 stringObject 中的位置。stringObject 中的字符位置是从 0 开始的,一定是所给位置向前寻找的第一个出现的字符位置。不存在返回-1

ps:对大小写敏感

a.lastIndexOf('cd')//10
a.lastIndexOf(f, a.length-1)//-1

d.includes(str ,index);(es6)指示str是否包含在字符串对象中

str:要查找的字符串/字符;

index:查找的起始位置;(可选)

返回:true/false

ps:

对大小写敏感

为了弥补indexof不够语义化的缺点

a.includes('f')//false
a.includes('f',-100)//false
a.includes('cd')//true
a.includes('cd',10)//true
a.includes('cd',-100)//true

e.startsWith(str,index)(es6)判断str是否在原字符串的头部

str:要查找的字符串/字符;

index:查找的起始位置,可以取负值(可选)

返回:true/false

ps:

对大小写敏感

借鉴其他语言

a.startsWith('cd')//false
a.startsWith('cd',2)//true
a.startsWith('cd',-1)//false
a.startsWith('ab',-1)//true
a.startsWith('a',-2)//true

f.endsWith(str,index)(es6)判断str是否在原字符串的尾部

str:要查找的字符串/字符;

index:查找的起始位置,可以取负值(可选)

  从index位置起往前开始查找  

  范围0 到 a.length - 1

  如果省略默认从最后一个字符处开始检索

返回:true/false

a.endsWith('b')//true
a.endsWith('a')//false
a.endsWith('a',1)//true

g.search(str/reg),match(str/reg) 可以与正则表达式相结合使用的,我就不多介绍了,match方法是可以单独写一篇使用方法的,以后再补上。

3.截取

1.substring(start,end)提取字符串中介于两个指定下标之间的字符

参数均非负整数,给下标就行,end至少比start多1,end(可选)省略默认返回的子串会一直从start到字符串的结尾

a.substring(0)//"abcd1234abcdab"
a.substring(1)//"bcd1234abcdab"
a.substring(1,1)//""
a.substring(1,100)//"bcd1234abcdab"
a.substring()//"abcd1234abcdab"

2.substr(start,length)抽取从 start 下标开始的指定数目的字符

start:正整数/负整数/0

  为负整数时,会从负整数位置向后截取

length: 正整数(可选)

  省略默认返回从 start的开始位置到结尾的字串。

a.substr()//"abcd1234abcdab"
a.substr(0,1)//"a"
a.substr(-1, 5)//"b"
a.substr(-10, 5)//"1234a"
a.substr(-100, 5)//"abcd1"---仔细看看

3.slice(start,end)可提取字符串的某个部分,并以新的字符串返回被提取的部分。

参数均正整数/负整数/0,给下标就行,end至少比start多1,end(可选)省略默认返回的子串会一直从start到字符串的结尾

a.slice(5)//"234abcdab"
a.slice(-1)//"b"
a.slice(1,-1)//"bcd1234abcda"
a.slice(-1,-1)//""
a.slice(-2,-1)//"a"
a.slice(-2,a.length)//"ab"

4.字符串转换为数组

1.split(separator,howmany)把一个字符串分割成字符串数组

separator:字符串或正则表达式,从该参数指定的地方分割a。

howmany:指定返回的数组的最大长度(可选)

  这么解释肯定不清楚,直接看栗子

a.split()//["abcd1234abcdab"]
a.split("")// ["a", "b", "c", "d", "1", "2", "3", "4", "a", "b", "c", "d", "a", "b"]
a.split(",")//["abcd1234abcdab"]字符串里没有的字符是不起作用的
a.split("",3)// ["a", "b", "c"]
//绕点来了
a.split("a",3)// ["", "bcd1234", "bcd"]
a.split("a",4) ["", "bcd1234", "bcd", "b"]

2.Array.of()(es6)将给定值转换为数组

5.遍历

a.split("")//1毛钱特效
Array.prototype.slice.call(a, 0)
Array.from(a)
//均返回["a", "b", "c", "d", "1", "2", "3", "4", "a", "b", "c", "d", "a", "b"]

/**
 *j解释下Array.from()将一个类数组对象或者可遍历对象转换成一个真正的数组
 *类数组对象最基本的要求就是拥有length属性的对象
 */

 

posted @ 2018-04-11 00:12  ELSE-IF  阅读(189)  评论(0)    收藏  举报