模版字符串
上代码
const name = "cool";
const age = 20;
const isMan = true;
const intro = `${name}的年龄是${age}, 他的性别是${isMan ? "男" : "女"}。`;
console.log("intro:", intro);
模版字符串主要就是在一段包含在``这里面的一段字符串, 字符串里面如果需要使用其他的变量,就使用 ${变量} 方法, 不同与以往的字符串通过 “+” 拼接, 并且在花括号里面还可以对 javascript 的表达式进行计算。
新增的常用字符串方法
1 includes 方法
判断字符串是否在已有的字符串中, 返回布尔值。
2 startsWith
判断原字符串开头是不是给定的字符串。
3 endsWith
判断原字符串结尾是不是给定的字符串。
const str = "cool is so cool a man that a lot of girls love him";
const isIncludeGirls = str.includes("girls");
const isStartsWithCool = str.startsWith("cool");
const isEndsWithsHe = str.endsWith("he");
console.log("isIncludeGirls: ", isIncludeGirls);
console.log("isStartsWithCool: ", isStartsWithCool);
console.log("isEndsWithsHe: ", isEndsWithsHe);