当前位置:网站首页>字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
2022-07-06 12:51:00 【viceen】
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
1、startsWith()方法
- 用来判断字符串是否以固定数据开头。
'abc'.startsWith('a') //true
'abc'.startsWith('d') //false
- 该方法还有第二个参数,可以从字符串指定位置开始判断,默认为0
'abcdefg'.startsWith('bcd')) //false
'abcdefg'.startsWith('bcd',1)) //true
2、endsWith()方法
- 第二个参数为所选字符串指定长度
'abc'.endsWith('c') //true
'abc'.endsWith('bc') //true
'abc'.endsWith('a') //false
'abcdefg'.endsWith('def')) //false
'abcdefg'.endsWith('def',6)) //true
3、trim()方法
- 从原始字符串的开头和结尾删除空格,中间的空格不进行处理。
- 并不影响原字符串本身,返回一个新字符串。
'Testing'.trim() //'Testing'
' Testing'.trim() //'Testing'
' Testing '.trim() //'Testing'
'Testing '.trim() //'Testing'
实例
<script>
var str = " yang ";
console.log(str);//输出 yang
var str1 = str.trim();//有返回值,要一个值接受
console.log(str1);//输出yang (没有空格)
var str2 = "ya ng";
console.log(str2);//输出"ya ng"
var str3 = str2.trim();
//从一个字符串的**两端**删除空白字符。字符串中间的空格不会删除
console.log(str3);//输出"ya ng"
</script>
使用正则表达式实现字符串的trim方法
String.prototype._trim = function() {
return this.replace(/^(\s*)|(\s*)$/g, '')
}
var str = ' ssss '
console.log(str) // 同上
console.log(str.length) // 11
var strNew = str._trim()
console.log(strNew) // 'ssss'
console.log(strNew.length) // 4
- 这里的*表示匹配0个或者多个,
- 此时我们需要考虑两种情况,一个是前面存在空格,另一个是后面存在空格。所以我们使用
|
来进行匹配。 - 并且使用
replace
来进行替换的话,只会替换第一个,所以我们需要加上全局匹配g
。
边栏推荐
- 【mysql】触发器
- How to implement common frameworks
- Core principles of video games
- 3D人脸重建:从基础知识到识别/重建方法!
- Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
- User defined current limiting annotation
- 爱可可AI前沿推介(7.6)
- Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
- KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
- [DSP] [Part 2] understand c6678 and create project
猜你喜欢
[diy] self designed Microsoft makecode arcade, official open source software and hardware
1_ Introduction to go language
数据湖(八):Iceberg数据存储格式
Application layer of tcp/ip protocol cluster
[DIY]自己设计微软MakeCode街机,官方开源软硬件
2022 portal crane driver registration examination and portal crane driver examination materials
[DSP] [Part 1] start DSP learning
What key progress has been made in deep learning in 2021?
Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
随机推荐
数据湖(八):Iceberg数据存储格式
7. Data permission annotation
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
OAI 5G NR+USRP B210安装搭建
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
2022 nurse (primary) examination questions and new nurse (primary) examination questions
监控界的最强王者,没有之一!
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
使用.Net驱动Jetson Nano的OLED显示屏
APS taps home appliance industry into new growth points
R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
Web开发小妙招:巧用ThreadLocal规避层层传值
Pycharm remote execution
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
966 minimum path sum
What key progress has been made in deep learning in 2021?
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
Yyds dry goods count re comb this of arrow function
[DIY]自己设计微软MakeCode街机,官方开源软硬件
Solution to the 38th weekly match of acwing