ES6中的字符串
字符串在编程语言中,一直是使用率很高的数据,虽说函数在js语言中是一等公民,但是字符串,怎么也能算得上是千年老二,下面我就一起和大家探讨一下在es6中的一些新用法和特性,特别是在字符串拼接的时候,会解放作为程序员的眼睛和双手。大家也可以关注我的微信公众号,蜗牛全栈。
一、模板字符串:用`替换传统的单引号或双引号
1、传统字符串拼接
const str = "asasasas\n"
+"sasasasasa\n"
+"rrgtrgtegergre"
console.log(str) // 这个时候输出的是换行的字符串
2、es6中字符串拼接
const str = `asasasas
sasasasasa
rrgtrgtegergre
`
console.log(str) // 输出的和上一段完全相同效果的字符串
3、传统字符串中的运算并拼接
const a = 20
const b = 14
const c = "es"
const str = "我的年龄是:"+(a+b)+",我在讲"+c
console.log(str) // 我的年龄是34,我在将es
4、es6字符串中的运算并拼接:在使用`的基础上,通过$和{}包裹变量。PS:这个语法在小编重构项目的时候,不知道省了多少眼药水,真是大大的福利呀。
const a = 20
const b = 14
const c = "es"
const str5 = `我的年龄是${a+b},我在讲${c}`
console.log() // 我的年龄是34,我在将es
二、嵌套模板:主要针对标签中的动态class。小编之前在Vue项目中看到大量类似的实例,当时还不明白,现在才知道是个啥。
1、传统方式
const isLarge = () => {
return true
}
let class1 = 'icon'
class1 += isLarge()?' icon-big':" icon-small"
console.log(class1) // 'icon icon-big'
2、es6新方式
const isLarge = () => {
return true
}
const class1 = `icon icon-${isLarge()?' icon-big':" icon-small"}`
console.log(class1) // 'icon icon-big'
三、带标签的模板字符串
const foo = (a,b,c,d) =>{
console.log(a)
console.log(b)
console.log(c)
console.log(d)
}
foo(1,2,3,4) // 1 2 3 4
const name = "lilei"
const age = 34
foo`这是${name},他的年龄是${age}岁` // ["这是",",他的年龄是","岁"] lilei 34 undefind
四、includes:判断指定字符串是否在其他字符串内,存在返回true,不存在返回false
// indexOf 判断字符串中是否含有某个字符串,如果存在,返回该字符串对应的下标,不存在返回-1
const str = "1234"
console.log(str.indexOf("2")) // 1
const str = "1234"
console.log(str.includes("2")) // true
五、startWith:判断自定义字符串是否以指定字符串开始,返回布尔值
const str = "1234"
console.log(str.startsWith("12")) // true
六、endWith:判断自定字符串是否以指定字符串结束,返回布尔值
const str = "1234"
console.log(str.endtWith("12")) // false
七、repeat:重复指定字符串指定次数,返回新字符串
const str = "1234"
const newStr = str.repeat(3)
console.log(newStr) // 123412341234
ES6中的字符串的更多相关文章
- ES6中 对字符串增强
在曾经,我们只能用A.indexof(B)来判断A中是否含有B字符串: 现在在ES6中 有了: includes(), startswith(),endswith() reapt()重复次数: 输出 ...
- ES6中对字符串处理的优点
目录 1.字符的Unicode表示法 2.字符串的遍历接口 3.直接输入 U+2028 和 U+2029(分行符 和 分段符) 4.JSON.stringify() 的改造 1.字符的Unicode表 ...
- ES6中新增字符串方法,字符串模板
多了两个新方法 startsWith endsWith 返回的是一个Boolean值 let str='git://www.baidu.com/2123123'; if(str.startsWith( ...
- ES6中的模板字符串和新XSS Payload
ES6中的模板字符串和新XSS Payload 众所周知,在XSS的实战对抗中,由于防守方经常会采用各种各样严格的过滤手段来过滤输入,所以我们使用的XSS Payload也会根据实际情况作出各种各样的 ...
- ES6中字符串扩展
ES6中字符串扩展 ① for...of 遍历字符串: 例如: for(let codePoint of 'string'){ console.log(codePoint) } 运行结果: ② in ...
- ES6中字符串的扩展
一.查找字符串 在ES5中,可以使用 indexOf 方法和 lastIndexOf 方法查找字符串: let str = 'hello world'; alert(str.indexOf('o')) ...
- es6中的模版字符串
目录 字符串拼接 includes() startsWith() endsWith() padStart() es6中的模版字符串替代了原有的字符串拼接功能. 字符串拼接 es5方式 传统的字符串拼接 ...
- ES6中字符串的新增方法梳理
1.String.fromCodePoint(); String,fromCodePoint()方法可以认为是对String.fromCharCode()方法的扩展,这两个方法的共同点在于都是用于Un ...
- ES6之模版字符串
ES6之模版字符串 最近在项目中使用了ES6的模版字符串,在这里加以总结. 1.之前我们也可以使用JavaScript输出模版字符串,通常是下面这样的: $("#result"). ...
随机推荐
- 一个或多个listeners启动失败,更多详细信息查看对应的容器日志文件
碰到这个问题很多次,每次碰到都是去百度找.但是,不尽人意,好在最后还是解决了,所以写下总结. 报错内容: org.apache.catalina.core.StandardContext.startI ...
- 【vue】报错This dependency was not found
报错 ERROR Failed to compile with 1 errors 10:33:34 ├F10: PM┤ This dependency was not found: * @/views ...
- web.xml 基本配置(SSM maven项目)
<web-app> <display-name>Archetype Created Web Application</display-name> <!--we ...
- jquery常用操作整理
1.数据中添加或者删除指定元素 var arr=['red','yello','blue']; arr.push('green'); //添加元素 arr = $.grep(arr,functio ...
- COM组件对象模型基础
COM组件对象模型 COM组件对象模型是为了创建一种独立于任何编程语言的对象.COM对象提供统一的接口,在不同的编程环境中通过调用COM对象特定接口的方法来完成特定的任务.一般有三种方式编写COM组件 ...
- Spring Cloud Gateway之动态路由(数据库版)
1.实现动态路由的关键是RouteDefinitionRepository接口,该接口存在一个默认实现(InMemoryRouteDefinitionRepository) 通过名字我们应该也知道该实 ...
- shell中 -eq,-ne,-gt,-lt,-ge,-le数字比较符
使用说明: -eq //equals等于 -ne //no equals不等于 -gt //greater than 大于 -lt //less than小于 -ge ...
- 马哥Linux--elasticsearch
ELK stack: Lucene: 文档:document 包含了一个或多个域的容器 field:value 域: 有很多选项 索引选项,存储选项,域向量使用选项 索引选项用于通过倒排索引来控制文本 ...
- Docker——Registry 通过Shell管理私有仓库镜像
使用方法: 复制代码保存为 image_registry.sh sh image_registry.sh -h #查看帮助 HUB=10.0.29.104:5000 改为自己的地址 #!/bin ...
- 运维常用shell脚本二(压缩文件、过滤不需要的文件、检测进程)
一.压缩指定目录下的文件并删除原文件 #!/bin/bashZIP_DAY=7 function zip { local dir=$1 if [ -d $dir ];then local file_n ...