当前位置:网站首页>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
.
边栏推荐
- Word bag model and TF-IDF
- 966 minimum path sum
- 【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
- What is the difference between procedural SQL and C language in defining variables
- 防火墙基础之外网服务器区部署和双机热备
- This year, Jianzhi Tencent
- 【滑动窗口】第九届蓝桥杯省赛B组:日志统计
- Is this the feeling of being spoiled by bytes?
- OAI 5G NR+USRP B210安装搭建
猜你喜欢
Pycharm remote execution
基于STM32单片机设计的红外测温仪(带人脸检测)
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
968 edit distance
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Seven original sins of embedded development
Is it profitable to host an Olympic Games?
【OpenCV 例程200篇】220.对图像进行马赛克处理
随机推荐
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
数据湖(八):Iceberg数据存储格式
Word bag model and TF-IDF
C language games - three chess
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
Common English vocabulary that every programmer must master (recommended Collection)
Yyds dry inventory run kubeedge official example_ Counter demo counter
966 minimum path sum
##无yum源安装spug监控
[MySQL] trigger
Is it profitable to host an Olympic Games?
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
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
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
C language operators
R语言做文本挖掘 Part4文本分类
Why do job hopping take more than promotion?