当前位置:网站首页>《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
2022-07-04 17:32:00 【InfoQ】
let str = '0123456789'
let result1 = str.substr(2,5) // 从下标2开始截取,截取5位
let result2 = str.slice(2,5) // 从下标2开始截取,截取到下标5(不含下标5)
let result3 = str.substring(2,5) // 从下标2开始截取,截取到下标5(不含下标5)
console.log(result1) // 控制台打印:23456
console.log(result2) // 控制台打印:234
console.log(result3) // 控制台打印:234
let str = '0123456789'
let result1 = str.substr(-2,-5) // 相当于 str.substr(str.length-2,-5),相当于 str.substr(8,-5), 从下标8开始截取,截取-5位
let result2 = str.slice(-2,-5) // 相当于 str.substr(str.length-2,str.length-5),相当于 str.substr(8,5), 从下标8开始截取,截取到下标5(不含下标5)
let result3 = str.substring(-2,-5) // 相当于 str.substr(0,0)
// 因为截取下标的区间都不存在元素,所以打印都为空
console.log(result1) // 控制台打印:
console.log(result2) // 控制台打印:
console.log(result3) // 控制台打印:
let str = '0123456789' // str.length为10
let result1 = str.substr(2) // 相当于 str.substr(2,str.length),相当于 str.substr(2,10),不足10位取到最后一位
let result2 = str.slice(2) // 相当于 str.slice(2,str.length),相当于 str.slice(2,10)
let result3 = str.substring(2) // 相当于 str.substring(2,10)
console.log(result1) // 控制台打印:23456789
console.log(result2) // 控制台打印:23456789
console.log(result3) // 控制台打印:23456789
let str = '0123456789' // str.length为10
let result1 = str.substr(-2) // 相当于 str.substr(str.length-2,str.length),相当于 str.substr(8,10),不足10位取到最后一位
let result2 = str.slice(-2) // 相当于 str.slice(str.length-2,str.length),相当于 str.slice(8,10)
let result3 = str.substring(-2) // 相当于 str.substring(0,10)
console.log(result1) // 控制台打印:89
console.log(result2) // 控制台打印:89
console.log(result3) // 控制台打印:0123456789
let str = '0123456789' // str.length为10
let result1 = str.substr(2,-5) // 从下标2开始截取,截取-5个字符,为空
let result2 = str.slice(2,-5) // 从下标2开始截取,截取到str.length-5,即相当于 str.slice(2,5)
let result3 = str.substring(2,-5) // 相当于 str.substring(0,2) ,即当substring的第一个参数为正数,第二个参数为负数时,则会把第一个参数作为0,第二个参数改为第一个参数的值即2。
console.log(result1) // 控制台打印:
console.log(result2) // 控制台打印:234
console.log(result3) // 控制台打印:01
let str = '0123456789' // str.length为10
let result1 = str.substr(-2,5) // 相当于 str.substr(str.length-2,5),相当于 str.substr(8,5)
let result2 = str.slice(-2,5) // 相当于 str.slice(str.length-2,5),相当于 str.substr(8,5),为空
let result3 = str.substring(-2,5) // 相当于 str.substring(0,5),即当substring的第一个参数为负数时,则会把第一个参数作为0。
console.log(result1) // 控制台打印:89
console.log(result2) // 控制台打印:
console.log(result3) // 控制台打印:01234
写在后面
边栏推荐
- 千万不要只学 Oracle、MySQL!
- 力扣刷題日記/day6/6.28
- [go ~ 0 to 1] read, write and create files on the sixth day
- repeat_P1002 [NOIP2002 普及组] 过河卒_dp
- Li Kou brush question diary /day8/7.1
- Machine learning concept drift detection method (Apria)
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- 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
- Scala basic tutorial -- 20 -- akka
- 完善的js事件委托
猜你喜欢
2022年字节跳动日常实习面经(抖音)
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
Scala basic tutorial -- 20 -- akka
Scala basic tutorial -- 17 -- Collection
力扣刷题日记/day7/6.30
Wireshark抓包TLS协议栏显示版本不一致问题
Scala基础教程--17--集合
奥迪AUDI EDI INVOIC发票报文详解
随机推荐
力扣刷题日记/day3/2022.6.25
Process of manually encrypt the mass-producing firmware and programming ESP devices
6.26cf simulation match B: solution to array reduction problem
Scala基础教程--19--Actor
Li Kou brush question diary /day7/6.30
力扣刷题日记/day8/7.1
php伪原创api对接方法
MXNet对GoogLeNet的实现(并行连结网络)
From automation to digital twins, what can Tupo do?
Interpretation of SIGMOD '22 hiengine paper
How to improve development quality
建立自己的网站(15)
Li Kou brush question diary /day8/7.1
一、C语言入门基础
模板_大整数减法_无论大小关系
Using FTP
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Perfect JS event delegation
How to download files using WGet and curl
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习