今天晚上整理了一下JavaScript的字符串函数,在vscode编辑器里使用智能语法提示进行练习,写下来以备参考。
字符串函数很多支持连写,比如concat、substring、substr、slice等等,类似链式编程,可以理解,字符串也是对象,结果返回自身。
1、concat(常用)
将两个或多个字符的文本组合起来,返回一个新的字符串。
console.log('=========concat函数');
var str1_1='JavaScript';//字符串对象
var str1_2='字符串函数';
var str1_3='举例';
var str1_4=str1_1.concat(str1_2).concat(str1_3);
console.log(str1_4);//输出:JavaScript字符串函数举例
var strList1=['JavaScript','AAA'];//数组对象
var strList2=['字符串函数','BBB'];
var strList3=['举例','CCC'];
var strList4=strList1.concat(strList2).concat(strList3);
console.log('=========concat函数');
console.log(strList4);//输出:(6) ["JavaScript", "AAA", "字符串函数", "BBB", "举例", "CCC"]
console.log(strList4.toString());//输出:JavaScript,AAA,字符串函数,BBB,举例,CCC
2、charAt(常用)
返回指定位置的字符。
var str4="JavaScript字符串函数";
console.log('=========charAt函数');
console.log(str4.charAt(0)+str4.charAt(3));//输出:Ja
console.log(str4.charAt(10));//输出:字
3、indexOf(常用)
返回字符串中一个子串第一处出现的索引(从左到右),没有匹配项则返回 -1 。
var str5='JavaScript字符串函数';
console.log('=========indexOf函数');
console.log(str5.indexOf('V'));//输出:-1,注意大小写匹配。
console.log(str5.indexOf('v'));//输出:2
console.log(str5.indexOf('函数'));//输出:13
4、substring(常用)
返回字符串的一个子串,传入参数是起始位置和结束位置。
var str6='JavaScript字符串函数';
console.log('=========substring函数');
console.log(str6.substring(0,4));//输出:Java
console.log(str6.substring(0,4).substring(1,2));//输出:a
console.log(str6.substring(4,7));//输出:Scr
5、substr(常用)
返回字符串的一个子串,传入参数是起始位置和结束位置。
var str6_1='JavaScript字符串函数';
console.log('=========substr函数');
console.log(str6_1.substr(0,4));//输出:Java
console.log(str6_1.substr(0,4).substr(1,2));//输出:av
console.log(str6_1.substr(4,7));//输出:Script字
●注意substr和substring的不同点
①substr(startIndex,lenth):第二个参数是截取字符串的长度(从起始点截取某个长度的字符串)。
②substring(startIndex, endIndex): 第二个参数是截取字符串最终的下标(截取2个位置之间的字符串,‘含头不含尾’)。
6、slice
提取字符串的一部分,并返回一个新字符串。
var str6='JavaScript字符串函数';
console.log('=========slice函数');
console.log(str6.slice(1));//输出:avaScript字符串函数,省略后面的参数则提取第一个外的后面所有字符
console.log(str6.slice(1,3));//输出:av
console.log(str6.slice(0,4));//输出:Java
console.log(str6.slice(0,4).slice(1,2));//输出:a
console.log(str6.slice(4,7));//输出:Scr
7、split(常用)
通过将字符串划分成子串,将一个字符串做成一个字符串数组。
console.log('=========split函数');
var str7='JavaScript 字符串 函数';
console.log(str7.split(' '));//输出:(3) ["JavaScript", "字符串", "函数"]
var str7='Java-Script-字符串=函数';
console.log(str7.split('-'));//输出:(3) ["Java", "Script", "字符串=函数"]
8、length(常用)
返回字符串的长度,所谓字符串的长度是指其包含的字符的个数。
var str8='JavaScript字符串函数';
console.log('=========length函数');
console.log(str8.length);//输出:15
9、toLowerCase
将整个字符串转成小写字母。
var str9='JavaScript字符串函数';
console.log('=========toLowerCase函数');
console.log(str8.toLowerCase());//输出:javascript字符串函数
10、toUpperCase
将整个字符串转成大写字母。
var str10='JavaScript字符串函数';
console.log('=========toUpperCase函数');
console.log(str10.toUpperCase());//输出:JAVASCRIPT字符串函数
11、replace
用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串。
var str10='JavaScript字符串函数';
var regExp=/JavaScript/g;
var outStr1=str10.replace(regExp,'C#')
console.log('=========replace函数');
console.log(outStr1);//输出:C#字符串函数
12、search
执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。
var str11='JavaScript字符串函数';
var regExp1=/Script/g;
console.log('=========search函数');
console.log(str11.search(regExp1));//输出:4
13、repeat
执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。
var str12='JS';
console.log('=========repeat函数');
console.log(str12.repeat(3));//输出:JSJSJS
14、startsWith
用来查找字符串某个位置上是否出现某个字符,注意大小写。
var str14='JavaScript';
console.log('=========startsWith函数');
console.log(str14.startsWith('v',2));//输出:true
console.log(str14.startsWith('V',2));//输出:false
console.log(str14.startsWith('va',2));//输出:true
15、endsWith
用来查找字符串结尾位置上是否出现某个字符,注意大小写。
console.log('=========endsWith函数');
var str17='JavaScript';
console.log(str17.endsWith("t",22));//输出:true
console.log(str17.endsWith("t",9));//输出:false
console.log(str17.endsWith("t",10));//输出:true
console.log(str17.endsWith("a",10));//输出:false
16、trim
去除字符串的前后空格
console.log('=========trim函数');
var str15_1=' JavaScript ';
var str15_2=' 字符串函数 ';
console.log(str15_1.trim()+str15_2.trim());//输出:JavaScript字符串函数(去掉了前后的空格)
17、includes
用来查找某个位置上是否出现某个字符,注意大小写。
console.log('=========includes函数');
var str16='JavaScript';
console.log(str16.includes('v',2));//输出:true
console.log(str16.includes('V',2));//输出:false
console.log(str16.includes("ve",2));//输出:false
console.log(str16.includes("va",2));//输出:true
18、charCodeAt
得到字符串指定位置上的ASCII码
var str18='JAvaScript';
console.log('=========includes函数');
console.log(str18.charCodeAt(1));//输出:65
console.log(str18.charCodeAt(3));//输出:97
19、lastIndexOf
返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
var str19='JavaScript';
console.log(str18.lastIndexOf('a'));//输出:3
后面要对正则表达式进行总结,还有经常用到的字符串转数字函数。