当前位置:网站首页>《看完就懂系列》字符串截取方法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
写在后面
边栏推荐
- Journal des problèmes de brosse à boutons de force / day6 / 6.28
- Esp32-c3 introductory tutorial questions ⑫ - undefined reference to ROM_ temp_ to_ power, in function phy_ get_ romfunc_ addr
- 资料下载 丨首届腾讯技术开放日课程精华!
- Rookie post station management system based on C language
- How is the entered query SQL statement executed?
- Wireshark packet capturing TLS protocol bar displays version inconsistency
- 6.26cf simulation race e: solution to the problem of price maximization
- 发送和接收IBM WebSphere MQ消息
- [cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
- Using SSH
猜你喜欢
![[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors

Grain Mall (I)

NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
![[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0

How to modify icons in VBS or VBE
![[go ~ 0 to 1] read, write and create files on the sixth day](/img/cb/b6785ad7d7c7df786f718892a0c058.png)
[go ~ 0 to 1] read, write and create files on the sixth day

Li Kou brush question diary /day7/6.30

【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现

Li Kou brush question diary /day3/2022.6.25

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
随机推荐
Scala基础教程--15--递归
[uniapp] uniapp development app online Preview PDF file
Interpretation of SIGMOD '22 hiengine paper
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
Send and receive IBM WebSphere MQ messages
【uniapp】uniapp开发app在线预览pdf文件
What if the self incrementing ID of online MySQL is exhausted?
学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
[209] go language learning ideas
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
Li Kou brush question diary /day7/2022.6.29
C语言打印练习
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Li Kou brush question diary /day8/7.1
神经网络物联网是什么意思通俗的解释
Li Kou brush question diary /day5/2022.6.27
Scala基础教程--13--函数进阶
Grain Mall (I)
Technology sharing | interface testing value and system