先点赞后关注,防止会迷路
寄语:没有一个冬天不会过去,没有一个春天不会到来。
前言字符串常用的属性和方法属性方法实例讲解length属性字符方法charAt()和charCodeAt()字符串操作方法concat()、slice()、substr()、substring()字符串位置方法indexOf()和lastIndexOf()trim()方法字符串大小写转换方法toLowerCase()和toUpperCase()字符串的模式匹配方法split()、match()、replace()、search()结尾
前言
字符串是一种非常重要的数据类型,在Java等面向对象编程语言中,它代表对象类型,而在javascript中它却是一种基本数据类型,在开发的领域中,我们经常会碰到,无论是前端还是后台。比如后台验证手机号码,将手机号码的后四位变成*,这些都是对字符串的处理。所以学会字符串中常用的属性和方法是非常必要的,本篇博客将带你解析字符串常用的属性和方法。那么一起来看看吧!
字符串常用的属性和方法
属性
- length:返回字符串的长度
方法
- chatAt():返回在指定位置的字符
- charCodeAt():返回在指定位置字符的unicode编码(ASCII编码)
- concat():连接字符串
- indexOf():从字符串的开头向后搜索字符串
- lastIndexOf():从字符串的末尾向前搜索字符串
- match():找到一个或多个正则表达式的匹配
- replace():替换与正则表达式匹配的字串
- search():检索与正则表达式相匹配的值
- slice():提取字符串的片段,并在新的字符串中返回被提取的部分
- split():把字符串分割成字符串数组
- substr():从起始索引号提取字符串中指定数目的字符
- substring():截取字符串中两个指定的索引号之间的字符
- toLowerCase():将字符串转换为小写
- toUpperCase():将字符串转换为大写
- toString():返回字符串本身
- valueOf():返回某个对象的原始值
- trim():删除前置及后缀的所有空格
实例讲解
length属性
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <title>字符串的length属性</title>
6 </head>
7 <body>
8 <script type="text/javascript">
9 //1:创建字符串
10 var str1=new String('Hello World');//通过new关键字
11 var str2='Hello World';//字面量
12 console.log(str1.length);//长度为11
13 console.log(str2.length);//长度为11
14 </script>
15 </body>
16</html>
tips:length代表返回字符串的长度。
字符方法charAt()和charCodeAt()
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <title>字符方法</title>
6 </head>
7 <body>
8 <script type="text/javascript">
9 var str='Hello World';//创建字符串
10 //1:测试charAt()方法
11 console.log(str.charAt(1));//返回e
12 //2:测试charCodeAt()方法
13 console.log(str.charCodeAt(1));//返回101(ASCII编码)
14 console.log(str[1]);//返回e
15 </script>
16 </body>
17</html>
字符串操作方法concat()、slice()、substr()、substring()
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <title>字符串的操作方法</title>
6 </head>
7 <body>
8 <script type="text/javascript">
9 //1:测试concat()方法
10 var str1='Hello ';
11 var result=str1.concat('World');
12 console.log(str1); //Hello
13 console.log(result);//Hello World
14
15 //2:测试slice(startIndex,[lastIndex])方法
16 //参数:开始下标,结束下标(可选)
17 var stringValue='hello world';
18 console.log(stringValue.slice(3));//lo world
19 console.log(stringValue.slice(3,7));//lo w
20
21 //3:测试substr(startIndex,[lastIndex])方法
22 //参数:开始下标,结束下标(可选)
23 console.log(stringValue.substr(3));//lo world
24 console.log(stringValue.substr(3,7));// lo worl
25
26 //4:测试substring(startIndex,[lastIndex])方法
27 //参数:开始下标,结束下标(可选)
28 console.log(stringValue.substring(3));//lo world
29 console.log(stringValue.substring(3,7));//lo w
30
31 var item='hello world';
32 console.log(item.slice(-3));//rld
33 console.log(item.substr(-3));//rld
34 console.log(item.substring(-3));//hello world
35 console.log(item.slice(3,-4));//lo w
36 console.log(item.substr(3,-4));//''空字符串
37 console.log(item.substring(3,-4));//hel
38 </script>
39 </body>
40</html>
这三个方法都返回被操作字符串的一个字符串,而且也接受一个或两个参数,当接受两个参数时,不包含结束下标,第一个参数指定字符串的起始位置,第二个参数(在指定的情况下)表示子字符串到哪里结束,具体来说,slice()和substring()的第二个参数指定的是字符串最后一个字符后面的位置,而substr()的第二个参数指定的则是返回的字符个数。如果没有给这些方法指定第二个参数,则将字符串的末尾作为结束位置。
在传递这些方法的参数是负值的情况下,它们的行为就不尽相同了,其中slice()方法会将传入的负值与字符串长度相加,substr()方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0。最后,substring()方法会将所有负值参数转换为0。
字符串位置方法indexOf()和lastIndexOf()
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <title>字符串位置方法</title>
6 </head>
7 <body>
8 <script type="text/javascript">
9 var stringValue='hello world';
10 //1:测试inexOf()方法
11 console.log(stringValue.indexOf('o'));//4
12 console.log(stringValue.indexOf('o',6));//7
13 //2:测试lastIndexOf()方法
14 console.log(stringValue.lastIndexOf('o'));//7
15 console.log(stringValue.lastIndexOf('o',6));//4
16
17 var item='Lorem ipsum dolor sit amet, consectetur adipisicing elit';
18 var positions=new Array();
19 var pos=item.indexOf('e');
20 while(pos>1){
21 positions.push(pos);
22 pos=item.indexOf('e',pos+1);
23 }
24 console.log(positions);//3,24,32,35,52;
25 </script>
26 </body>
27</html>
trim()方法
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <title>trim()方法</title>
6 </head>
7 <body>
8 <script type="text/javascript">
9 var str=' hello world ';
10 var trimStr=str.trim();
11 console.log(str);// hello world
12 console.log(trimStr);//hello world
13 </script>
14 </body>
15</html>
字符串大小写转换方法toLowerCase()和toUpperCase()
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <title>字符串大小写方法</title>
6 </head>
7 <body>
8 <script type="text/javascript">
9 var str='Hello World';
10 console.log(str.toLowerCase()); //hello world
11 console.log(str.toUpperCase());//HELLO WORLD
12 console.log(str.toLocaleLowerCase());//hello world
13 console.log(str.toLocaleUpperCase());//HELLO WORLD
14 </script>
15 </body>
16</html>
字符串的模式匹配方法split()、match()、replace()、search()
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta charset="UTF-8">
5 <title>字符串的模式匹配方法</title>
6 </head>
7 <body>
8 <script type="text/javascript">
9 //1:测试match()方法
10 var text1='cat, bat, sat, fat';
11 var pattern=/.at/;
12 var matches=text1.match(pattern);
13 console.log(matches.index);//0
14 console.log(matches[0]);//cat
15 console.log(pattern.lastIndex);//0
16
17 //2:测试search()方法
18 var text2='cat bat, sat, fat';
19 var pos=text2.search(/at/);
20 console.log(pos);//1
21
22 //3:测试replace()方法
23 var text3='cat, bat, sat, fat';
24 var result=text3.replace('at','ond');
25 console.log(result);//cond,bat,sat,fat
26 result =text3.replace(/at/g,'ond');
27 console.log(result);//cond,bond,sond,fond
28
29 //4:测试split()方法
30 var text4='red,blue,green,yellow';
31 var colors1=text4.split(',');
32 var colors2=text4.split(',',2);
33 console.log(colors1);//['red','blue','green','yellow'];
34 console.log(colors2);//['red','blue'];
35
36
37 </script>
38 </body>
39</html>
tips:
match()方法
match()方法本质上与调用RegExp的exec()方法相同,match()方法只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。
search()方法
search()方法与match()方法的参数相同,有字符串或RegExp对象指定的一个正则表达式,search()方法返回字符串中第一个匹配项的索引,如果没有找到匹配项,则返回-1,而且,search()方法始终从字符串开头向后匹配查找模式。
replace()方法
replace()方法接收两个参数,第一个参数可以是一个RegExp对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参数可以可以是一个字符串或者一个函数。如果第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字符串,唯一的方法就是提供一个正则表达式,而且要指定全局(g)标志。
spilt()方法
split()方法可以基于指定的分隔符将一个字符串分割成多少个字符串,并将结果放在数组中。分隔符可以是字符串,也可以是一个RegExp对象(这个方法不会将字符串看成正则表达式)。split()方法可以接受可选的第二个参数,用于指定数组的大小,以确保返回的数组不会超过既定大小。
结尾
如果觉得本篇文章对您有用的话,麻烦您给笔者点亮那个点赞按钮。
对于二太子木吒这个暖男来说:真的非常有用,您的支持就是我前进的动力,我们下篇文章见。
原创不易,请勿白嫖。
二太子木吒,一个在互联网前端苟且偷生的小白一枚,专注于前端开发,善于分享技术。
如需转载,请联系作者或者保留原文链接,微信公众号搜索二太子木吒