与es6的不期而遇 - 字符串

5 篇文章 0 订阅
4 篇文章 0 订阅

模版字符串

上代码

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);

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值