当前位置:网站首页>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
.
边栏推荐
- el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
- Yyds dry inventory run kubeedge official example_ Counter demo counter
- Regular expression collection
- Reflection operation exercise
- Spark SQL chasing Wife Series (initial understanding)
- New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
- el-table表格——sortable排序 & 出现小数、%时排序错乱
- Aike AI frontier promotion (7.6)
- js之遍历数组、字符串
- [sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
猜你喜欢
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
None of the strongest kings in the monitoring industry!
20220211 failure - maximum amount of data supported by mongodb
2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
Reinforcement learning - learning notes 5 | alphago
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
Why do job hopping take more than promotion?
随机推荐
What is the difference between procedural SQL and C language in defining variables
968 edit distance
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
Nodejs tutorial let's create your first expressjs application with typescript
OSPF multi zone configuration
JS操作dom元素(一)——获取DOM节点的六种方式
Thinking about agile development
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
[wechat applet] operation mechanism and update mechanism
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
Nodejs教程之Expressjs一篇文章快速入门
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
3D人脸重建:从基础知识到识别/重建方法!
Word bag model and TF-IDF
How to implement common frameworks
Why do job hopping take more than promotion?
Vim 基本配置和经常使用的命令
快过年了,心也懒了