当前位置:网站首页>ES6-字符串
ES6-字符串
2022-07-31 05:18:00 【春意迟迟、】
ES6判断字符串是否包含子串:
- includes():返回布尔值,判断是否找到参数字符串。
startsWith():返回布尔值,判断参数字符串是否在原字符串的头部。
endsWith():返回布尔值,判断参数字符串是否在原字符串的尾部
<script> let string = "hello,world!"; string.includes("llo"); // true 是否包含 string.startsWith("he"); // true 以..开头 string.endsWith("or"); // false 以..结尾 string.startsWith(",", 5) // true 下标为x的位置是否以..开头 </script>(1)以上三个方法都可以有两个参数:需要搜索的字符串,(可选)搜索起始位置索引
(2)这三个方法只返回布尔值,如果需要知道子串的位置,还是得用 indexOf 和 lastIndexOf 。
(3)这三个方法如果传入了正则表达式而不是字符串,会抛出错误。而 indexOf 和 lastIndexOf 这两个方法,它们会将正则表达式转换为字符串并搜索它。
字符串重复:
<script> // 参数是重复的次数 console.log("abcd".repeat(3)) // 参数是小数,向下取整 console.log("abcd".repeat(3.6)) //如果参数是NaN 或是0-1,-1-0之间的小数 则重复0次 console.log("abcd".repeat(0.6)) console.log("abcd".repeat(NaN)) //如果参数是-1到负无穷之间的负数或者 Infinity ,会报错 console.log("abcd".repeat(-2.6)) console.log("abcd".repeat(Infinity)) </script>
字符串补全:
padStart:返回新的字符串,表示用参数字符串从头部补全原字符串。
<script> console.log("hello".padStart(7,"o")); // "oohello" </script>padStart的第一个参数是新字符串的长度,第二个参数是用什么来补全字符串,如果没有指定第二个参数,默认用空格填充。
padEnd:返回新的字符串,表示用参数字符串从尾部补全原字符串。
<script> console.log("hello".padEnd(7,"o")); // "hellooo" </script>padEnd的第一个参数是新字符串的长度,第二个参数是用什么来补全字符串,如果没有指定第二个参数,默认用空格填充。
注:如果指定的长度小于或等于原字符串的长度,则返回原字符串
如果原字符串加上补全字符串长度大于指定长度,则截去超出位数的补全字符串
模板字符串:
<script> //普通字符串 let string = `Hello'\n'world`; console.log(string); //hello 换行 world //多行字符串 let string1 = `today is a good day`; console.log(string1); //变量名写在 ${} 中,${} 中可以放入 JavaScript 表达式。 //${}可以取出变量的值 let name = "Mike"; let age = 27; let info = `My Name is ${name},I am ${age + 1} years old next year.` console.log(info); //字符串中调用函数 function fn() { return "good day"; } let string2 = `today is a ${fn()}`; console.log(string2); // today is agood day </script>注:模板字符串相当于加强版的字符串,用反引号 `
模板字符串中的换行和空格都是会被保留的。
边栏推荐
- Getting Started with MySQL: The Case Statement Works Well
- MySQL master-slave switching steps
- C语言静态变量static
- DSPE-PEG-Thiol DSPE-PEG-SH phospholipid-polyethylene glycol-thiol liposome for later use
- IDEA控制台不能输入信息的解决方法
- 911崩了,自养号测评环境IP有哪些更好的选择
- 关于网络安全法的个人理解
- Picture-in-Picture API in the browser
- Word vector - demo
- CSDN上markdown编写的一些便捷操作
猜你喜欢

911崩了,自养号测评环境IP有哪些更好的选择

pyspark.ml feature transformation module

Unity Text一个简单的输入特效

CAS:1403744-37-5 DSPE-PEG-FA 科研实验用磷脂-聚乙二醇-叶酸

Chemical Reagent Phospholipid-Polyethylene Glycol-Amino, DSPE-PEG-amine, CAS: 474922-26-4

堆和栈的区别

DSPE-PEG-Azide DSPE-PED-N3 Phospholipid-Polyethylene Glycol-Azide Lipid PFG

TCP与UDP协议的区别,以及TCP的三次握手和四次挥手

PyTorch Study Notes 08 - Loading Datasets

Webrtc从理论到实践二: 架构
随机推荐
力扣刷题之排序链表
C语言对文件的操作(完整版)
Pytorch study notes 10 - detailed explanation of convolutional neural network and application of multi-classification task of mnist dataset
About iframe
Natural language processing related list
Phospholipids-Polyethylene Glycol-Active Esters for Scientific Research DSPE-PEG-NHS CAS: 1445723-73-8
Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
Wlan实验(ENSP)
Tensorflow steps on the pit while using it
测试CSDN积分需求
在 AWS 上从零开始设置 Incredibuild 构建系统
会话和饼干,令牌
企业级 DevOps 究竟是什么?
CAS: 1403744-37-5 DSPE-PEG-FA Phospholipid-Polyethylene Glycol-Folic Acid for Scientific Research
【Rhapsody学习笔记】4:Relations
Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
C语言静态变量static
5G的用途和工作原理
Pytorch Daily Practice - Predicting Surviving Passengers on the Titanic
记一次QT 2D 画图 实现3D动态效果