String对象方法concat/startWith/endsWith/indexOf/lastIndexOf/valueOf/includes
concat:连接字符串,连接字符串数组
let str = "hello world";
let str1 = "hey man. you are good!";
console.log(str.concat(str1));
console.log(str.concat(["lucy", "kobe", "jam"]));
startWith:断字符串是否以某个字符或字符串开始,第二个参数表示从字符串第几位开始匹配
const str1 = 'i am student and teacher!';
console.log(str1.startsWith('i am'));
const str3 = "hello world"
console.log(str3.startsWith('lo', 3));
const str2 = 'Is this your grid friend?';
console.log(str2.startsWith('s this'));
endsWith:判断字符串是否以某个字符或字符串结尾,第二个参数表示从字符串第几位开始匹配
const str1 = 'i am student and teacher!';
console.log(str1.endsWith('teacher!'));
const str3 = "hello world"
console.log(str3.endsWith('lo', 5));
const str2 = 'Is this your grid friend?';
console.log(str2.endsWith('friend'));
includes:判断字符串是否包含指定字符或字符串,第二个参数表示从字符串第几位开始匹配
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be'));
console.log(str.includes('question'));
console.log(str.includes('nonexistent'));
console.log(str.includes('To be', 1));
console.log(str.includes('TO BE'));
indexOf:判断字符串是否包含指定字符或字符串,返回对应的索引位置,没找到返回-1,第二个参数表示从字符串第几位开始匹配
var str = 'To be, or not to be, that is the question.';
console.log(str.indexOf('To be'));
console.log(str.indexOf('question'));
console.log(str.indexOf('nonexistent'));
console.log(str.indexOf('To be', 1));
console.log(str.indexOf('TO BE'));
lastIndexOf:判断字符串是否包含指定字符或字符串,从尾部开始查找,返回对应的索引位置,没找到返回-1,第二个参数表示从字符串第几位开始匹配
var str = 'To be, or not to be, that is the question.';
console.log(str.lastIndexOf('at is'));
console.log(str.lastIndexOf('not '));
console.log(str.lastIndexOf('nonexistent'));
console.log(str.lastIndexOf('To be', 1));
console.log(str.lastIndexOf('TO BE'));
valueOf:从对象中获取字符串
const stringObj = new String('foo');
console.log(stringObj);
console.log(stringObj.valueOf());