当前位置:网站首页>The use method of string is startwith () - start with XX, endswith () - end with XX, trim () - delete spaces at both ends
The use method of string is startwith () - start with XX, endswith () - end with XX, trim () - delete spaces at both ends
2022-07-06 21:10:00 【viceen】
How to use strings startwith()- With XX start 、endsWith()- With XX ending 、trim()- Delete spaces at both ends
1、startsWith() Method
- Used to determine whether the string starts with fixed data .
'abc'.startsWith('a') //true
'abc'.startsWith('d') //false
- The method also has a second parameter , You can judge from the specified position of the string , The default is 0
'abcdefg'.startsWith('bcd')) //false
'abcdefg'.startsWith('bcd',1)) //true
2、endsWith() Method
- The second parameter specifies the length for the selected string
'abc'.endsWith('c') //true
'abc'.endsWith('bc') //true
'abc'.endsWith('a') //false
'abcdefg'.endsWith('def')) //false
'abcdefg'.endsWith('def',6)) //true
3、trim() Method
- From the original string The beginning and the end Delete the blank space , The space in the middle is not processed .
- It does not affect the original string itself , Returns a new string .
'Testing'.trim() //'Testing'
' Testing'.trim() //'Testing'
' Testing '.trim() //'Testing'
'Testing '.trim() //'Testing'
example
<script>
var str = " yang ";
console.log(str);// Output yang
var str1 = str.trim();// There is a return value , To accept a value
console.log(str1);// Output yang ( There are no spaces )
var str2 = "ya ng";
console.log(str2);// Output "ya ng"
var str3 = str2.trim();
// From a string of ** Both ends ** Delete white space characters . The space in the middle of the string will not be deleted
console.log(str3);// Output "ya ng"
</script>
Use regular expressions to realize string trim Method
String.prototype._trim = function() {
return this.replace(/^(\s*)|(\s*)$/g, '')
}
var str = ' ssss '
console.log(str) // ditto
console.log(str.length) // 11
var strNew = str._trim()
console.log(strNew) // 'ssss'
console.log(strNew.length) // 4
- there * Represents a match 0 One or more ,
- At this point, we need to consider two situations , One is that there is a space in front , The other is that there is a space after . So we use
|
To match . - And use
replace
To replace it , Only the first one will be replaced , So we need to add global matchingg
.
边栏推荐
- js通过数组内容来获取数组下标
- [200 opencv routines] 220 Mosaic the image
- [wechat applet] operation mechanism and update mechanism
- 快过年了,心也懒了
- 面试官:Redis中有序集合的内部实现方式是什么?
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
- Is this the feeling of being spoiled by bytes?
- Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
- Swagger UI tutorial API document artifact
猜你喜欢
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
基于深度学习的参考帧生成
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
Aike AI frontier promotion (7.6)
3D人脸重建:从基础知识到识别/重建方法!
SAP UI5 框架的 manifest.json
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
This year, Jianzhi Tencent
监控界的最强王者,没有之一!
爱可可AI前沿推介(7.6)
随机推荐
Deployment of external server area and dual machine hot standby of firewall Foundation
Forward maximum matching method
Three schemes of SVM to realize multi classification
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
What key progress has been made in deep learning in 2021?
Nodejs教程之Expressjs一篇文章快速入门
Mtcnn face detection
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
过程化sql在定义变量上与c语言中的变量定义有什么区别
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
User defined current limiting annotation
Performance test process and plan
Nodejs tutorial expressjs article quick start
愛可可AI前沿推介(7.6)
【mysql】触发器
如何实现常见框架
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
Yyds dry goods count re comb this of arrow function
Ravendb starts -- document metadata
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀