es6基础系列四--字符串的拓展
1 for...of 字符串的遍历接口
for(let i of "abc"){
console.log(i);
}
// a
// b
// c
2 includes 是否包含某字符串,返回布尔值
格式:str.includes(searchString[, position])
与indexOf的对比:
indexOf:返回下标,判断是否包含某字符串,下标是字符串的位置
includes:返回布尔值,是否包含某字符串,如果只是判断字符串中包含,此法可行。
var s = "hello";
// es5
s.indexOf("o"); // 4
// es6
s.includes("o"); // true
s.includes("d"); // false
s.includes("h", 2); // false 从第三个字符开始找
3 startsWith 参数字符串是否在源字符串的头部,返回布尔值
格式:str.startsWith(searchString[, position])
var s = "hello world";
// es5
s.indexOf("hello"); // 0 等于0表示就在源字符串头部
// es6
s.startsWith("hello"); // true
s.startsWith("world"); // false
s.startsWith("world", 6); // true
4 endsWith 跟startsWith相反,表示参数字符串是否在源字符串的尾部,返回布尔值
格式:str.endsWith(searchString[, position])
var s = "hello world";
// es5
String.prototype.endWith=function(endStr){
var d=this.length-endStr.length;
return (d>=0&&this.lastIndexOf(endStr)==d)
}
s.endWith("world"); // true
// es6
s.endsWith("world"); // true
s.endsWith("world", 5); // false
s.endsWith("hello", 5); // true
5 repeat 将原字符串重复n次,返回一个新字符串
var s = "s";
s.repeat(3); // sss
s.repeat(2.6); // ss 小数会被取整
s.repeat(-2); // RangeError 报错
s.repeat(0); // ""
6 模板字符串 是增强版的字符串,用反引号(`)标识。
它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量,好处相当明显,不用再拼接字符串,使用模板字符串内部可以使用变量了。
// es5 输出模板通常是如下格式,相当繁琐还不方便
var name="Bob",time="today";
var resultStr = "hello "+name+", how are you "+time+'?'; //hello Bob, how are you today?
// es6 模板字符串
console.log(`string text line 1
string text line 2`);
//string text line 1
//string text line 2
// 直接用${变量名}表示
`Hello ${name}, how are you ${time}?` // Hello Bob, how are you today?
// 使用表达式
var obj={a:1,b:2};
`${obj.a+obj.b}` // 3
// 使用函数
function fn() {
return "Hello World";
}
`this is fn return str: ${fn()}` // this is fn return str: Hello World
具体es6关于字符串的变化、拓展请查看MDN官网
es6基础系列四--字符串的拓展的更多相关文章
- es6基础系列五--数组的拓展
Array.from() 用于将两类对象转为真正的数组,类似数组对象和可遍历对象(包括数据结构Set和Map)转化为数组 格式:Array.from(arrayLike[, mapFn[, thisA ...
- 【C++自我精讲】基础系列四 static
[C++自我精讲]基础系列四 static 0 前言 变量的存储类型:存储类型按变量的生存期划分,分动态存储方式和静态存储方式. 1)动态存储方式的变量,生存期为变量所在的作用域.即程序运行到此变量时 ...
- 夯实基础系列四:Linux 知识总结
前言 前三节内容传送门: 夯实基础系列一:Java 基础总结 夯实基础系列二:网络知识总结 夯实基础系列三:数据库知识总结 现在很多公司项目部署都使用的是 Linux 服务器,互联网公司更是如此.对于 ...
- C#夯实基础系列之字符串
string作为我们在编程当中用的最多的数据类型,同时又由于它的特殊性,怎么强调它的重要性都不为过,理解string的一些类型和存储机制,有助于我们写出正确且高效的代码. 一.string类型 1.s ...
- 从零开始学 Web 之 ES6(六)ES6基础语法四
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- ES6入门系列四(测试题分析)
0.导言 ES6中新增了不少的新特性,来点测试题热热身.具体题目来源请看:http://perfectionkills.com/javascript-quiz-es6/. 以下将一题一题来解析what ...
- ES6标准入门之字符串的拓展讲解
在开始讲解ES6中字符串拓展之前,我们先来看一下ES5中字符串的一些方法. 获取字符串长度 str.length 分割字符串 str.split() 拼接字符串 str1+str2 或 str1.co ...
- es6基础系列二:Number
es6中关于Number类型的变化,主要有以下几点 Number.isInteger(新增,判断是否为整数) Number.EPSILON(新增,判断是否可忽略的误差) Number.MAX_SAFE ...
- mysql 开发基础系列5 字符串函数
字符串函数 1. concat (s1,s2,...sn) 连接里面的参数成一个字符串(注意上面写错了函数名称) SELECT CONCAT('ddd','CCC'); 2. insert(str ...
随机推荐
- hbase shell-namespace(命名空间指令)
hbase shell命名空间namespace篇: 1. alter_namespace (一般用于添加删除一个属性,很少使用) hbase(main)::> help 'alter_name ...
- Vim 替换命令
一,":substitute"的使用 :substitute 命令可以对一个指定范围的区域执行替换操作,可以简写为:s ,它的通用形式如下: :[range]substitute/ ...
- 算法(Algorithms)第4版 练习 2.2.10
关键代码实现: private static void merge(Comparable[] input, int lo, int mid, int hi) { //copy input[lo,mid ...
- a note of R software write Function
Functionals “To become significantly more reliable, code must become more transparent. In particular ...
- (转)edm注意事项
格式编码 1.页面宽度请设定在600到800px以内,长度1024px以内. 2.HTML编码请使用utf-8. 3.HTML代码在15KB以内.(各个邮箱的收件标准不一样,如果超出15KB您的邮件很 ...
- 将double型小数点后面多余的零去掉
/** 函数功能:将数值小数点后面多余的零清空.* 参数描述:* [in] aSource - 输入的源数值:* [out] aDestination - 输出截取后的数值* ...
- Java_异常_04_ OutOfMemoryError系列
二.参考资料 1.铁猫 OutOfMemoryError系列(1): Java heap space OutOfMemoryError系列(2): GC overhead limit exceeded ...
- numpy.ndarray类型的数组元素输出时,保留小数点后4位
因为计算结果数组中每个值都是很长的一串小数,看起来比较乱,想格式化一下输出方式. 这是个看起来很简单的问题,但是方法找了很久. 方法也是看起来很简单,用 numpy.set_printoptions( ...
- Skype SILK vs. iLBC vs. Speex
对比一下这三种VOIP语音算法的特点: 1 参数与特征 2 SILK性能 关于iLBC和Speex的性能可以参考以前写的文章. 3 关于VOIP一些观点(仅代表个人观点) 1) Skype 辛苦三年 ...
- hdu 3998 Sequence
There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequence of X as x[i1], ...