当前位置:网站首页>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
边栏推荐
猜你喜欢
Deleting nodes in binary search tree
千万不要只学 Oracle、MySQL!
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
使用canal配合rocketmq监听mysql的binlog日志
2022 ByteDance daily practice experience (Tiktok)
Scala基础教程--20--Akka
从实时应用角度谈通信总线仲裁机制和网络流控
升级智能开关,“零火版”、“单火”接线方式差异有多大?
随机推荐
Using SSH
repeat_P1002 [NOIP2002 普及组] 过河卒_dp
Lex and yacc based lexical analyzer + parser
2021 合肥市信息学竞赛小学组
[uniapp] uniapp development app online Preview PDF file
Basic tutorial of scala -- 16 -- generics
模板_判断素数_开方 / 六素数法
BI技巧丨权限轴
The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
神经网络物联网是什么意思通俗的解释
TorchDrug教程
工厂从自动化到数字孪生,图扑能干什么?
OpenCV的二值化处理函数threshold()详解
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
Deleting nodes in binary search tree
Cache é JSON uses JSON adapters
2022 ByteDance daily practice experience (Tiktok)
Have you guys ever used CDC direct Mysql to Clickhouse
Caché JSON 使用JSON适配器
Send and receive IBM WebSphere MQ messages