当前位置:网站首页>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
边栏推荐
- From automation to digital twins, what can Tupo do?
- C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
- Bi skills - permission axis
- IBM WebSphere MQ retrieving messages
- 26. 删除有序数组中的重复项 C#解答
- Caché WebSocket
- Angry bird design based on unity
- Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
- 性能优化之关键渲染路径
- Caché JSON 使用JSON适配器
猜你喜欢

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

Scala基础教程--15--递归

更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?

基于lex和yacc的词法分析器+语法分析器

Wireshark网络抓包

Mxnet implementation of googlenet (parallel connection network)

性能优化之关键渲染路径

Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
![[release] a tool for testing WebService and database connection - dbtest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[release] a tool for testing WebService and database connection - dbtest v1.0

Scala basic tutorial -- 17 -- Collection
随机推荐
Scala basic tutorial -- 19 -- actor
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
One question per day (2022-07-02) - Minimum refueling times
Pb extended DLL development (super chapter) (VII)
性能优化之关键渲染路径
Rookie post station management system based on C language
SSL证书续费相关问题详解
[release] a tool for testing WebService and database connection - dbtest v1.0
工厂从自动化到数字孪生,图扑能干什么?
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
奥迪AUDI EDI INVOIC发票报文详解
读写关闭的channel是啥后果?
学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join
Torchdrug tutorial
国元期货是正规平台吗?在国元期货开户安全吗?
2022 ByteDance daily practice experience (Tiktok)
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason