您好,欢迎访问一九零五行业门户网

对JS操作字符串的方法汇总

1:concat() 将两个或多个字符的文本组合起来,返回一个新的字符串
1 var f1=hello;
2 var f2=world;
3 document.write(f1.concat(f2))  //hello world
2:indexof() 返回字符串中一个子串第一处出现的索引。如果没有匹配项,返回 -1
1 var f3=hello world
2 console.log(f3.indexof('world')) //6
3 console.log(f3.indexof('world')) //-1
4 console.log(f3.indexof('hello')) //0
注意:indexof区分大小写
3:charat() – 返回指定位置的字符。
1 var f4=hello world;
2 console.log(f4.charat(1))   //e
3 console.log(f4)  //hello world
4:lastindexof() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
1 var f3=hello world
2 console.log(f3.lastindexof('world')) //6
3 console.log(f3.lastindexof('world')) //-1
4 console.log(f3.lastindexof('hello')) //0
5:substring() – 返回字符串的一个子串。传入参数是起始位置和结束位置(不必需)。注:参数不能为负数
1 var f5=hello world
2 console.log(f5.substring(3)) //lo world
3 console.log(f5.substring(3,8))  //lo wo
6:match() – 检查一个字符串是否匹配一个正则表达式。 
7:replace() – 用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串。
8:search() – 执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。
9:slice() – 提取字符串的一部分,并返回一个新字符串。 (参数可以为负数)
1 var f6=hello world
2 console.log(f6.slice(6)) //world
3 console.log(f6.slice(6,9)) //wor
10:split() – 方法用于把一个字符串分割成字符串数组。 
1 var f7=hello world;
2 console.log(f7.split()); //[h, e, l, l, o, , w, o, r, l, d]
3 console.log(f7.split( ));  //[hello, world]
4 console.log(f7.split( ,1)); //[hello]
11:tolowercase() – 将整个字符串转成小写字母。 
12:touppercase() – 将整个字符串转成大写字母。
以上就是对js操作字符串的方法汇总的详细内容。
其它类似信息

推荐信息