当前位置:网站首页>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.
边栏推荐
- c#使用oracle存储过程获取结果集实例
- Mtcnn face detection
- Data Lake (VIII): Iceberg data storage format
- 039. (2.8) thoughts in the ward
- Pycharm remote execution
- PG基础篇--逻辑结构管理(事务)
- 数据湖(八):Iceberg数据存储格式
- js中,字符串和数组互转(二)——数组转为字符串的方法
- KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
- 嵌入式开发的7大原罪
猜你喜欢
![[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

【mysql】触发器

968 edit distance

HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅

3D人脸重建:从基础知识到识别/重建方法!

What key progress has been made in deep learning in 2021?

1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效

ICML 2022 | flowformer: task generic linear complexity transformer

Redis insert data garbled solution

1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
随机推荐
How to turn a multi digit number into a digital list
Opencv learning example code 3.2.3 image binarization
Reflection operation exercise
Why do job hopping take more than promotion?
什么是RDB和AOF
Reinforcement learning - learning notes 5 | alphago
1_ Introduction to go language
#yyds干货盘点#重新梳理箭头函数的this
面试官:Redis中有序集合的内部实现方式是什么?
Mtcnn face detection
Reference frame generation based on deep learning
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
OneNote in-depth evaluation: using resources, plug-ins, templates
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
Spark SQL chasing Wife Series (initial understanding)
[MySQL] basic use of cursor
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
Web开发小妙招:巧用ThreadLocal规避层层传值