当前位置:网站首页>The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
2022-07-04 19:22:00 【InfoQ】
let str = '0123456789'
let result1 = str.substr(2,5) // From the subscript 2 Began to intercept , Intercept 5 position
let result2 = str.slice(2,5) // From the subscript 2 Began to intercept , Intercept to subscript 5( Without subscript 5)
let result3 = str.substring(2,5) // From the subscript 2 Began to intercept , Intercept to subscript 5( Without subscript 5)
console.log(result1) // Console printing :23456
console.log(result2) // Console printing :234
console.log(result3) // Console printing :234
let str = '0123456789'
let result1 = str.substr(-2,-5) // amount to str.substr(str.length-2,-5), amount to str.substr(8,-5), From the subscript 8 Began to intercept , Intercept -5 position
let result2 = str.slice(-2,-5) // amount to str.substr(str.length-2,str.length-5), amount to str.substr(8,5), From the subscript 8 Began to intercept , Intercept to subscript 5( Without subscript 5)
let result3 = str.substring(-2,-5) // amount to str.substr(0,0)
// Because there is no element in the interval where the subscript is intercepted , So the print is empty
console.log(result1) // Console printing :
console.log(result2) // Console printing :
console.log(result3) // Console printing :
let str = '0123456789' // str.length by 10
let result1 = str.substr(2) // amount to str.substr(2,str.length), amount to str.substr(2,10), Insufficient 10 Take the last bit
let result2 = str.slice(2) // amount to str.slice(2,str.length), amount to str.slice(2,10)
let result3 = str.substring(2) // amount to str.substring(2,10)
console.log(result1) // Console printing :23456789
console.log(result2) // Console printing :23456789
console.log(result3) // Console printing :23456789
let str = '0123456789' // str.length by 10
let result1 = str.substr(-2) // amount to str.substr(str.length-2,str.length), amount to str.substr(8,10), Insufficient 10 Take the last bit
let result2 = str.slice(-2) // amount to str.slice(str.length-2,str.length), amount to str.slice(8,10)
let result3 = str.substring(-2) // amount to str.substring(0,10)
console.log(result1) // Console printing :89
console.log(result2) // Console printing :89
console.log(result3) // Console printing :0123456789
let str = '0123456789' // str.length by 10
let result1 = str.substr(2,-5) // From the subscript 2 Began to intercept , Intercept -5 Characters , It's empty
let result2 = str.slice(2,-5) // From the subscript 2 Began to intercept , Intercept to str.length-5, That is equivalent to str.slice(2,5)
let result3 = str.substring(2,-5) // amount to str.substring(0,2) , When substring The first parameter of is a positive number , When the second parameter is negative , The first parameter will be taken as 0, The second parameter is changed to the value of the first parameter, that is 2.
console.log(result1) // Console printing :
console.log(result2) // Console printing :234
console.log(result3) // Console printing :01
let str = '0123456789' // str.length by 10
let result1 = str.substr(-2,5) // amount to str.substr(str.length-2,5), amount to str.substr(8,5)
let result2 = str.slice(-2,5) // amount to str.slice(str.length-2,5), amount to str.substr(8,5), It's empty
let result3 = str.substring(-2,5) // amount to str.substring(0,5), When substring When the first parameter of is negative , The first parameter will be taken as 0.
console.log(result1) // Console printing :89
console.log(result2) // Console printing :
console.log(result3) // Console printing :01234
Written in the back
边栏推荐
- Detailed explanation of issues related to SSL certificate renewal
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- 6.26cf simulation match B: solution to array reduction problem
- How to modify icons in VBS or VBE
- 读写关闭的channel是啥后果?
- 2022年字节跳动日常实习面经(抖音)
- Mxnet implementation of googlenet (parallel connection network)
- Send and receive IBM WebSphere MQ messages
- [mathematical basis of machine learning] (I) linear algebra (Part 1 +)
- Scala基础教程--13--函数进阶
猜你喜欢

DeFi生态NFT流动性挖矿系统开发搭建

从实时应用角度谈通信总线仲裁机制和网络流控

LM10丨余弦波动顺势网格策略

Rookie post station management system based on C language

Nebula importer data import practice
![[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

Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join

Scala基础教程--13--函数进阶

Scala basic tutorial -- 13 -- advanced function

2022CoCa: Contrastive Captioners are Image-Text Fountion Models
随机推荐
Build your own website (15)
26. Delete the duplicate item C solution in the ordered array
1672. 最富有客户的资产总量
LeetCode第300场周赛(20220703)
千万不要只学 Oracle、MySQL!
读写关闭的channel是啥后果?
Deleting nodes in binary search tree
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
The 15th youth informatics competition in Shushan District in 2019
Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
资料下载 丨首届腾讯技术开放日课程精华!
Mxnet implementation of googlenet (parallel connection network)
Rookie post station management system based on C language
26. 删除有序数组中的重复项 C#解答
What if the self incrementing ID of online MySQL is exhausted?
Perfect JS event delegation
整理混乱的头文件,我用include what you use
模板_判断素数_开方 / 六素数法
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
Using SSH