当前位置:网站首页>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
边栏推荐
- How is the entered query SQL statement executed?
- 876. Intermediate node of linked list
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- 2022CoCa: Contrastive Captioners are Image-Text Fountion Models
- Scala basic tutorial -- 17 -- Collection
- 信息学奥赛一本通 1336:【例3-1】找树根和孩子
- How to modify icons in VBS or VBE
- 【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
- C language printing exercise
- 2022CoCa: Contrastive Captioners are Image-Text Fountion Models
猜你喜欢
物联网应用技术的就业前景和现状
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
Rookie post station management system based on C language
Lex and yacc based lexical analyzer + parser
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
Scala basic tutorial -- 19 -- actor
2022年字节跳动日常实习面经(抖音)
OpenCV的二值化处理函数threshold()详解
Scala基础教程--15--递归
Principle and application of ThreadLocal
随机推荐
性能优化之关键渲染路径
Caché WebSocket
Mxnet implementation of googlenet (parallel connection network)
2022年字节跳动日常实习面经(抖音)
Rookie post station management system based on C language
Leetcode fizzbuzz C # answer
One question per day (2022-07-02) - Minimum refueling times
Wireshark packet capturing TLS protocol bar displays version inconsistency
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
Leetcode ransom letter C # answer
Go microservice (II) - detailed introduction to protobuf
从实时应用角度谈通信总线仲裁机制和网络流控
Caché WebSocket
LeetCode第300场周赛(20220703)
[go ~ 0 to 1] read, write and create files on the sixth day
技术分享 | 接口测试价值与体系
Have you guys ever used CDC direct Mysql to Clickhouse
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
Scala基础教程--14--隐式转换
2022 ByteDance daily practice experience (Tiktok)