针对字符串的操作方法

javaScript中String类型数据存在有多种用来操作字符串的方法,平时的使用过程中也会使用非常频繁,是非常重要的部分。
包括将字符串拆分成字符,字符串的拼接截取,字符串的查找以及字符串大小写形式的转化等;

1.字符方法

用于访问字符串中特定字符的方法:charAt()和charCodeAt().这两个方法都接收一个参数,即基于0的字符位置。

1
2
var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"

如果你想得到是字符编码

1
2
var stringValue = "hello world";
alert(stringValue.charCodeAt(1)); //输出"101"

另外在ECMAScript5中定义了另一个访问个别字符的方法

1
2
var stringValue = "hello world";
alert(stringValue[1]); //"e"(在某些浏览器版本中可能不尽相同)

2.字符串方法

前面学习Array数组类型的数据存在slice()方法,这里先与String类型的slice()方法作一下对比:

①slice()方法

String类型的slice()方法与Array类型slice()方法对比

String类型slice()方法:
该方法会返回一个原字符串的一个子字符串,该函数接受一个或者两个参数,指定字符串开始的位置和结束的位置(都是基于0开始的),并且不包括结束位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 let a = "hello world";
let sl = a.slice(2); //截取下标为2以后的位置
console.log(sl); //llo world

let sl2 = a.slice(1,2); //截取下标1到下标2的位置,包前不包后
console.log(sl2); //e

//如果为负数的情况
let sl3 = a.slice(-2); //截取-2位置至末尾(这里的倒数的下标不是从0开始的请注意)
console.log(sl3); //ld

let sl4 = a.slice(-2,0); //并不能正确的截取到
console.log(sl4); //空

let sl5 = a.slice(-2,-1); //并不能正确的截取到
console.log(sl5); //l

我们再来看看Array类型的slice()方法:
基于当前数组中的一个或者多个项创建一个新数组。slice()方法可以接受一或两个参数,即要返回项的起始和结束为位置;
一个参数的情况:slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。
两个参数的情况:slice()返回起始和结束位置之间的项-但不包括结束位置的项。
参数其实就是数组中项对应的下标,如果传入的是负数那么则可以按照加上数组的长度的方式继续转化,也可以理解为从数组的末尾进行倒数定位;

1
2
3
4
5
6
7
8
9
10
11
12
13
var colors = ["red","green","blue","yellow","purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);

alert(colors2); //green,blue,yellow,purple
alert(colors3); //green,bule,yellow

//负数的形式
var colors = ["red","green","blue","yellow","purple"];
var colors2 = colors.slice(-1);
var colors3 = colors.slice(-2,-1);
console.log(colors2); //purple
console.log(colors3); //yellow

总结:总的来说,两者的用法几乎是一样的,只不过一个是用在数组上,一个是用在字符串上,字符串也可以用来对比数组进行使用。

②concat()方法

用于字符串的拼接

1
2
3
4
var stringValue = "hello "; 
var result = stringValue.concat("world");
alert(result); //"hello world"
alert(stringValue); //"hello" 原字符串不受影响

concat()方法可以接受任意多个参数,也就是说可以通过它拼接任意多个字符串。

1
2
3
4
var stringValue = "hello "; 
var result = stringValue.concat("world", "!");
alert(result); //"hello world!"
alert(stringValue); //"hello"

③substr()和substring()

ECMAScript还提供了三个基于子字符串创建新字符串的方法:slice()substr()substring(),在上面我们已经介绍了slice(),在这里我们将这三个函数再次进行对比。

1
2
3
4
5
6
7
var stringValue = "hello world"; 
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl"

三者都是接受一个或者两个参数,slice()与substring():都是传入起始位置、结束位置,substr():接收起始位置、和指定要返回的字符个数
当参数其中存在负数的时候,函数将会有不同的行为反应:

1
2
3
4
5
6
7
var stringValue = "hello world"; 
alert(stringValue.slice(-3)); //"rld"
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.substr(-3)); //"rld"
alert(stringValue.slice(3, -4)); //"lo w"
alert(stringValue.substring(3, -4)); //"hel"
alert(stringValue.substr(3, -4)); //""(空字符串)

slice():当参数为负数的时候将参数加上字符串长度,再进行返回
substring():当参数为负的时候,会将所有的负数参数转化为0
substr():将负的第一个参数加上字符串的长度,而将负的第二个参数转换为 0。

1
2
3
4
alert(stringValue.substring(3, -4)); 		//"hel" 
//转化为
alert(stringValue.substring(3, 0)); //同样返回"hel"
alert(stringValue.substring(0, 3)); //"hel"

W3C与菜鸟教程上面的说法与实际的操作并不相同,使用负数参数并不会出现报错;
substring
所以作为负数的入参形式虽然可行,可能并不建议这样的使用方式,容易造成混淆,需要注意。

3.字符串位置方法

两个查找字符串的方法indexOf()、lastIndexOf();
这两个方法都是从一个字符串中搜索给定的子字符串,然后返子字符串的位置(如果没有找到该子字符串,则返回-1)。
这两个方法的区别在于:indexOf()方法从字符串的开头向后搜索子字符串,而 lastIndexOf()方法是从字符串的末尾向前搜索子字符串。

1
2
3
var stringValue = "hello world"; 
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7

这两个方法都可以接收可选的第二个参数,表示从字符串中的哪个位置开始搜索。换句话说,indexOf()会从该参数指定的位置向后搜索,忽略该位置之前的所有字符;而 lastIndexOf()则会从
指定的位置向前搜索,忽略该位置之后的所有字符。

1
2
3
var stringValue = "hello world"; 
alert(stringValue.indexOf("o", 6)); //7
alert(stringValue.lastIndexOf("o", 6)); //4

3.trim()方法

所有的字符串都具有trim()方法,这个方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果;

1
2
3
4
var stringValue = " hello world "; 
var trimmedStringValue = stringValue.trim();
alert(stringValue); //" hello world "
alert(trimmedStringValue); //"hello world"

4.字符串大小写转换

ECMAScript 中涉及字符串大小写转换的方
法有 4 个:toLowerCase()、toLocaleLowerCase()、toUpperCase()和 toLocaleUpperCase()。
经常使用的要数toLowerCase()和toUpperCase(),转化成小写形式和大写形式。