当前位置:网站首页>字符串的使用方法之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。
边栏推荐
- 1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
- Taylor series fast Fourier transform (FFT)
- Logic is a good thing
- Math symbols in lists
- 7、数据权限注解
- C language operators
- Common doubts about the introduction of APS by enterprises
- OSPF multi zone configuration
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
猜你喜欢

Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)

硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件

Value of APS application in food industry

Use of OLED screen

Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
![[diy] how to make a personalized radio](/img/fc/a371322258131d1dc617ce18490baf.jpg)
[diy] how to make a personalized radio

2017 8th Blue Bridge Cup group a provincial tournament

What is the problem with the SQL group by statement
![[MySQL] trigger](/img/b5/6df17eb254bbdb0aba422d08f13046.png)
[MySQL] trigger

Deployment of external server area and dual machine hot standby of firewall Foundation
随机推荐
#yyds干货盘点#重新梳理箭头函数的this
It's almost the new year, and my heart is lazy
##无yum源安装spug监控
Application layer of tcp/ip protocol cluster
Comment faire une radio personnalisée
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
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
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Implementation of packaging video into MP4 format and storing it in TF Card
【mysql】游标的基本使用
Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
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
知识图谱构建流程步骤详解
OAI 5g nr+usrp b210 installation and construction
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
Performance test process and plan
APS taps home appliance industry into new growth points
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
[MySQL] basic use of cursor