当前位置:网站首页>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
replaceTo replace it , Only the first one will be replaced , So we need to add global matchingg.
边栏推荐
- Web开发小妙招:巧用ThreadLocal规避层层传值
- Dynamically switch data sources
- Simple continuous viewing PTA
- 【论文解读】用于白内障分级/分类的机器学习技术
- 966 minimum path sum
- 3D人脸重建:从基础知识到识别/重建方法!
- Why do job hopping take more than promotion?
- How to implement common frameworks
- C # use Oracle stored procedure to obtain result set instance
- Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
猜你喜欢

This year, Jianzhi Tencent
![[asp.net core] set the format of Web API response data -- formatfilter feature](/img/6b/e3d513f63b244f9f32555d3b3bec8c.jpg)
[asp.net core] set the format of Web API response data -- formatfilter feature

Deployment of external server area and dual machine hot standby of firewall Foundation

全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀

KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning

2017 8th Blue Bridge Cup group a provincial tournament

Why do job hopping take more than promotion?

每个程序员必须掌握的常用英语词汇(建议收藏)

Pycharm remote execution
![[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics](/img/2d/9a7e88fb774984d061538e3ad4a96b.png)
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
随机推荐
User defined current limiting annotation
Regular expression collection
js中,字符串和数组互转(二)——数组转为字符串的方法
js中,字符串和数组互转(一)——字符串转为数组的方法
Data Lake (VIII): Iceberg data storage format
【mysql】触发器
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
7. Data permission annotation
C language games - minesweeping
Swagger UI tutorial API document artifact
爱可可AI前沿推介(7.6)
20220211 failure - maximum amount of data supported by mongodb
js通过数组内容来获取数组下标
PG基础篇--逻辑结构管理(事务)
OAI 5g nr+usrp b210 installation and construction
What is the problem with the SQL group by statement
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性