js substr、substring和slice使用说明小记
时间:2023-07-15 16:26:03
关于substr、substring和slice方法区别的文章,网上搜到了许多,文章内容也基本一致。而后,我将其中一篇文章中的代码挪到本地进行了测试,发现测试结果和原文中的有些出入。
我更相信自己亲自验证过后的代码,随后小记下来,供以后查阅。
substr
document.write("|" + str.substr(0,5) + "|" + "<br />");
document.write("|" + str.substr(0) + "|" + "<br />");
document.write("|" + str.substr(5,1) + "|" + "<br />");
document.write("|" + str.substr(-5,2) + "|" + "<br />");
document.write("|" + str.substr(-2,-5) + "|" + "<br />");
打印效果
|12345|
|123456|
|6|
IE: |12| Chrome: |23|
substring
document.write("|" + str.substring(0,5) + "|" + "<br />");
document.write("|" + str.substring(0) + "|" + "<br />");
document.write("|" + str.substring(5,1) + "|" + "<br />");
document.write("|" + str.substring(-5,2) + "|" + "<br />");
document.write("|" + str.substring(-2,-5) + "|" + "<br />");
document.write("|" + str.substring(2,-5) + "|" + "<br />");
打印效果
|12345|
|123456|
|2345|
|12|
|12|
slice
document.write("|" + str.slice(0,5) + "|" + "<br />");
document.write("|" + str.slice(0) + "|" + "<br />");
document.write("|" + str.slice(5,1) + "|" + "<br />");
document.write("|" + str.slice(-5,2) + "|" + "<br />");
document.write("|" + str.slice(-2,-5) + "|" + "<br />");
document.write("|" + str.slice(2,-5) + "|" + "<br />");
打印效果
|12345|
|123456|
|2|
||
如果你预想的结果和打印效果完全一致,那你的基本功一定不赖。如果多少有点犹豫或者结果出乎你的意料,那我的文章多少也有点用处了。
总结
substr
该方法的arg2和其它两个方法的arg2压根就代表不同意义,所以单独揪出来。
当arg1<0时,不同浏览器的结果不同。IE 直接将arg1改成0, Chrome 下标读取方式由原来的从左往右读改成从右往左读
substring和slice
(1)arg2 > arg1,即参数2大于参数1时
substring:自动颠倒位置,数值大的在arg2位置上,数值小的在arg1位置上
slice:返回空字符串
(2)对负数的处理
substring:将数值为负数的参数转换为0
slice:将数值为负数的参数转换为(字符串长度-参数数值)
标签:substr,substring,slice
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于ThinkPHP删除目录及目录文件函数
2024-06-05 09:46:36
PyTorch中的参数类torch.nn.Parameter()详解
2021-09-07 19:06:30
网页设计之文字的辨识度与可读性
2007-10-26 16:19:00
![](https://img.aspxhome.com/file/UploadPic/200710/26/20071026162228999s.gif)
基于PHP做个图片防盗链
2023-05-25 00:27:30
Vue 项目性能优化方案分享
2024-05-10 14:20:36
Python3爬虫学习之MySQL数据库存储爬取的信息详解
2024-01-19 23:06:19
![](https://img.aspxhome.com/file/2023/4/79634_0s.png)
关于tensorflow softmax函数用法解析
2022-10-29 07:42:09
![](https://img.aspxhome.com/file/2023/7/101117_0s.jpg)
springboot配置文件抽离 git管理统 配置中心详解
2021-05-24 08:17:18
解决bootstrap中使用modal加载kindeditor时弹出层文本框不能输入的问题
2024-04-16 09:26:21
HTML5 Web Storage全解析
2010-06-26 13:06:00
![](https://img.aspxhome.com/file/UploadPic/20106/26/01-15s.jpg)
python用matplotlib可视化绘图详解
2022-04-20 02:38:42
![](https://img.aspxhome.com/file/2023/7/72067_0s.png)
Django实现跨域请求过程详解
2022-08-31 23:45:56
![](https://img.aspxhome.com/file/2023/6/101006_0s.png)
Python实现子类调用父类的方法
2023-09-28 12:00:55
Python3+selenium配置常见报错解决方案
2022-12-21 14:44:53
python实现DEM数据的阴影生成的方法
2023-11-09 11:17:25
![](https://img.aspxhome.com/file/2023/4/135204_0s.png)
python的numpy模块安装不成功简单解决方法总结
2022-10-23 18:33:15
![](https://img.aspxhome.com/file/2023/6/66396_0s.png)
php查询mysql数据库并将结果保存到数组的方法
2023-07-20 17:47:32
Python全栈之进程和守护进程
2021-11-25 18:05:44
![](https://img.aspxhome.com/file/2023/1/70781_0s.png)
深入浅出Golang中的sync.Pool
2024-02-11 12:59:34
![](https://img.aspxhome.com/file/2023/8/112208_0s.png)
Python数据可视化Pyecharts库的使用教程
2023-01-24 23:17:34
![](https://img.aspxhome.com/file/2023/0/101320_0s.png)