字符串常用的属性和方法
一、字符串
1.属性 length
//1:创建字符串
var str1=new String('Hello World');//通过new关键字
console.log(str1.length);//长度为11
var str2='Hello World';//字面量
console.log(str2.length);//长度为11
2.拼接和截取 concat()、slice()、substr()、substring()
//1:测试concat()方法
var str1='Hello ';
var result=str1.concat('World');
console.log(str1); //Hello
console.log(result);//Hello World
//2:测试slice(startIndex,lastIndex)方法 // 返回值是被截取的字符串
//参数:开始下标,结束下标(可选) //包前不包后
var stringValue='';
console.log(stringValue.slice(3));//lo world
console.log(stringValue.slice(3,7));//lo w
//3:测试substr(startIndex,lastIndex)方法 // 返回值是被截取的字符串
//参数:开始下标,结束下标(可选) //第二个参数返回的字符个数
console.log(stringValue.substr(3));//lo world
console.log(stringValue.substr(3,4));// lo w
//4:测试substring(startIndex,lastIndex)方法 // 返回值是被截取的字符串
//参数:开始下标,结束下标(可选) //包前不包后
console.log(stringValue.substring(3));//lo world
console.log(stringValue.substring(3,7));//lo w
var item='hello world';
console.log(item.slice(-3));//rld //其中slice()方法会将传入的负值与字符串长度相加
console.log(item.slice(3,-4));//lo w
//substr()方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0
console.log(item.substr(-3));//rld
console.log(item.substr(3,-4));//''空字符串
//substring()方法会将所有负值参数转换为0。
console.log(item.substring(-3));//hello world
console.log(item.substring(3,-4));//hel
3.字符串位置 indexOf()和lastIndexOf()和charAt()
var stringValue='hello world';
//1:测试inexOf()方法
console.log(stringValue.indexOf('o'));//4
//1,两个参数第一个代表要检索的值(value),第二个值代表开始检索的位置(from)
console.log(stringValue.indexOf('o',6));//7
//2:测试lastIndexOf()方法
console.log(stringValue.lastIndexOf('o'));//7
console.log(stringValue.lastIndexOf('o',6));//4
//3:测试charAt()方法
console.log(stringValue.charAt('7'));//o
4.去除收尾空白 trim()方法
var str=' hello world ';
var trimStr=str.trim();
console.log(trimStr);//hello world
5.字符串大小写转换 toLowerCase()和toUpperCase()
var str='Hello World';
console.log(str.toLowerCase()); //hello world
console.log(str.toUpperCase());//HELLO WORLD
console.log(str.toLocaleLowerCase());//hello world
console.log(str.toLocaleUpperCase());//HELLO WORLD
6.字符串的模式匹配方法 split()、match()、replace()、search()
//1:测试match()方法
var text1='cat, bat, sat, fat';
var pattern=/.at/; //匹配除换行符 \n 之外的任何单字符。要匹配 . ,请使用 \. 。
var matches=text1.match(pattern);
console.log(matches.index);//0
console.log(matches[0]);//cat
//正则表达式开始下一次查找的索引位置,第一次的时候总是为0的
console.log(pattern.lastIndex);//0
//search()方法返回字符串中第一个匹配项的索引,如果没有找到匹配项,则返回-1,而且,search()方法始终从字符串开头向后匹配查找模式。
//2:测试search()方法
var text2='cat bat, sat, fat';
var pos=text2.search(/at/);
console.log(pos);//1
//3:测试replace()方法
var text3='cat, bat, sat, fat';
var result=text3.replace('at','ond');
console.log(result);//cond,bat,sat,fat
result =text3.replace(/at/g,'ond');
console.log(result);//cond,bond,sond,fond
//4:测试split()方法
var text4='red,blue,green,yellow';
var colors1=text4.split(',');
var colors2=text4.split(',',2);
console.log(colors1);//['red','blue','green','yellow'];
//split()方法可以接受可选的第二个参数,用于指定数组的大小,以确保返回的数组不会超过既定大小。
console.log(colors2);//['red','blue'];
链接: https://www.cnblogs.com/jjgw/p/11608617.html.
链接: https://blog.csdn.net/q46mu28wem/article/details/72629423.