当前位置:网站首页>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
边栏推荐
- 其他InterSystems %Net工具
- Basic tutorial of scala -- 16 -- generics
- 26. Delete the duplicate item C solution in the ordered array
- Unity editor extends C to traverse all pictures in folders and subdirectories
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- Leetcode fizzbuzz C # answer
- Scala基础教程--15--递归
- 2022年字节跳动日常实习面经(抖音)
- 876. Intermediate node of linked list
- MXNet对GoogLeNet的实现(并行连结网络)
猜你喜欢
随机推荐
LeetCode FizzBuzz C#解答
redis分布式锁的8大坑总结梳理
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
2021 合肥市信息学竞赛小学组
My colleagues quietly told me that flying Book notification can still play like this
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
LeetCode第300场周赛(20220703)
IBM WebSphere MQ检索邮件
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
node_exporter部署
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
DeFi生态NFT流动性挖矿系统开发搭建
Principle and application of ThreadLocal
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
2022 ByteDance daily practice experience (Tiktok)
[go ~ 0 to 1] read, write and create files on the sixth day
Li Chi's work and life summary in June 2022
One question per day (2022-07-02) - Minimum refueling times
基于C语言的菜鸟驿站管理系统
Esp32-c3 introductory tutorial questions ⑫ - undefined reference to ROM_ temp_ to_ power, in function phy_ get_ romfunc_ addr