当前位置:网站首页>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
边栏推荐
- LeetCode第300场周赛(20220703)
- Nebula importer data import practice
- Nebula Importer 数据导入实践
- Scala basic tutorial -- 12 -- Reading and writing data
- Wireshark网络抓包
- 6.26cf simulation match B: solution to array reduction problem
- ThreadLocal原理与使用
- 【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
- Li Chi's work and life summary in June 2022
- Scala basic tutorial -- 14 -- implicit conversion
猜你喜欢
从实时应用角度谈通信总线仲裁机制和网络流控
Nebula importer data import practice
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
Scala basic tutorial -- 20 -- akka
[go ~ 0 to 1] read, write and create files on the sixth day
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
Bi skills - permission axis
Don't just learn Oracle and MySQL!
基于unity的愤怒的小鸟设计
Scala基础教程--15--递归
随机推荐
1672. Total assets of the richest customers
26. Delete the duplicate item C solution in the ordered array
Torchdrug tutorial
Scala basic tutorial -- 13 -- advanced function
删除字符串中出现次数最少的字符【JS,Map排序,正则】
2019年蜀山区第十五届青少年信息学竞赛
Scala basic tutorial -- 20 -- akka
关于判断点是否位于轮廓内的一点思考
学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
LeetCode FizzBuzz C#解答
One question per day (2022-07-02) - Minimum refueling times
SSL证书续费相关问题详解
自由小兵儿
Scala基础教程--16--泛型
Scala basic tutorial -- 14 -- implicit conversion
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
PB的扩展DLL开发(超级篇)(七)
6.26CF模拟赛B:数组缩减题解
Caché WebSocket
升级智能开关,“零火版”、“单火”接线方式差异有多大?