当前位置:网站首页>《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
2022-07-04 17:32:00 【InfoQ】
let str = '0123456789'
let result1 = str.substr(2,5) // 从下标2开始截取,截取5位
let result2 = str.slice(2,5) // 从下标2开始截取,截取到下标5(不含下标5)
let result3 = str.substring(2,5) // 从下标2开始截取,截取到下标5(不含下标5)
console.log(result1) // 控制台打印:23456
console.log(result2) // 控制台打印:234
console.log(result3) // 控制台打印:234
let str = '0123456789'
let result1 = str.substr(-2,-5) // 相当于 str.substr(str.length-2,-5),相当于 str.substr(8,-5), 从下标8开始截取,截取-5位
let result2 = str.slice(-2,-5) // 相当于 str.substr(str.length-2,str.length-5),相当于 str.substr(8,5), 从下标8开始截取,截取到下标5(不含下标5)
let result3 = str.substring(-2,-5) // 相当于 str.substr(0,0)
// 因为截取下标的区间都不存在元素,所以打印都为空
console.log(result1) // 控制台打印:
console.log(result2) // 控制台打印:
console.log(result3) // 控制台打印:
let str = '0123456789' // str.length为10
let result1 = str.substr(2) // 相当于 str.substr(2,str.length),相当于 str.substr(2,10),不足10位取到最后一位
let result2 = str.slice(2) // 相当于 str.slice(2,str.length),相当于 str.slice(2,10)
let result3 = str.substring(2) // 相当于 str.substring(2,10)
console.log(result1) // 控制台打印:23456789
console.log(result2) // 控制台打印:23456789
console.log(result3) // 控制台打印:23456789
let str = '0123456789' // str.length为10
let result1 = str.substr(-2) // 相当于 str.substr(str.length-2,str.length),相当于 str.substr(8,10),不足10位取到最后一位
let result2 = str.slice(-2) // 相当于 str.slice(str.length-2,str.length),相当于 str.slice(8,10)
let result3 = str.substring(-2) // 相当于 str.substring(0,10)
console.log(result1) // 控制台打印:89
console.log(result2) // 控制台打印:89
console.log(result3) // 控制台打印:0123456789
let str = '0123456789' // str.length为10
let result1 = str.substr(2,-5) // 从下标2开始截取,截取-5个字符,为空
let result2 = str.slice(2,-5) // 从下标2开始截取,截取到str.length-5,即相当于 str.slice(2,5)
let result3 = str.substring(2,-5) // 相当于 str.substring(0,2) ,即当substring的第一个参数为正数,第二个参数为负数时,则会把第一个参数作为0,第二个参数改为第一个参数的值即2。
console.log(result1) // 控制台打印:
console.log(result2) // 控制台打印:234
console.log(result3) // 控制台打印:01
let str = '0123456789' // str.length为10
let result1 = str.substr(-2,5) // 相当于 str.substr(str.length-2,5),相当于 str.substr(8,5)
let result2 = str.slice(-2,5) // 相当于 str.slice(str.length-2,5),相当于 str.substr(8,5),为空
let result3 = str.substring(-2,5) // 相当于 str.substring(0,5),即当substring的第一个参数为负数时,则会把第一个参数作为0。
console.log(result1) // 控制台打印:89
console.log(result2) // 控制台打印:
console.log(result3) // 控制台打印:01234
写在后面
边栏推荐
- 工厂从自动化到数字孪生,图扑能干什么?
- 建立自己的网站(15)
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
- Mysql5.7 installation tutorial graphic explanation
- Scala基础教程--19--Actor
- Li Kou brush question diary /day4/6.26
- DeFi生态NFT流动性挖矿系统开发搭建
- How is the entered query SQL statement executed?
- [2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
- 6.26cf simulation race e: solution to the problem of price maximization
猜你喜欢

What if the self incrementing ID of online MySQL is exhausted?

每日一题(2022-07-02)——最低加油次数

How to modify icons in VBS or VBE

My colleagues quietly told me that flying Book notification can still play like this

Nebula Importer 数据导入实践

力扣刷题日记/day7/2022.6.29

小发猫物联网平台搭建与应用模型

正则替换【JS,正则表达式】

Mxnet implementation of googlenet (parallel connection network)

Li Kou brush question diary /day6/6.28
随机推荐
[go ~ 0 to 1] read, write and create files on the sixth day
Mysql5.7 installation tutorial graphic explanation
Li Kou brush question diary /day4/6.26
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
神经网络物联网应用技术就业前景【欢迎补充】
激进技术派 vs 项目保守派的微服务架构之争
Perfect JS event delegation
Is it safe to download the mobile version of Anxin securities and open an account online
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
My colleagues quietly told me that flying Book notification can still play like this
李迟2022年6月工作生活总结
Rookie post station management system based on C language
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
力扣刷题日记/day3/2022.6.25
基于NCF的多模块协同实例
ThreadLocal原理与使用
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
Interpretation of SIGMOD '22 hiengine paper
学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”