[javascript string] slice();substr();substring();之间的区别
2024-10-17 15:06:24
今天遇到这个问题,发现ぜんぜんわすねまます
3个方法,直接上代码吧,[网络版本较多就不注明参考过哪些了 -0- ]
var test = 'hello world'; //均一位参数测试
console.log(test.slice()); //llo world
console.log(test); //hello world
console.log(test.substr()); //llo world
console.log(test); //hello world
console.log(test.substring()); //llo world
console.log(test); //hello world
//---1个整数传参返回结果一样,亦不会影响原来的字符串
console.log(test.slice(-)); //ld
console.log(test); //hello world
console.log(test.substr(-)); //ld
console.log(test); //hello world
console.log(test.substring(-)); //hello world
console.log(test); //hello world
// //---1个负数传参不会影响原来的字符串
// //str.slice(a); str.substr(a);返回结果为字符串从右往左两位
// //而 str.substring();则返回字符串本身 //均两位参数测试
console.log(test.slice(,)); //o w
console.log(test); //hello world
console.log(test.substr(,)); //o world
console.log(test); //hello world
console.log(test.substring(,)); //o w
// console.log(test); //hello world
//第2位传参为正整数
//1.都不会改变原有str的长度
//2.str.slice(a,b); str.substring(a,b);
//均为下标0起,由下标a到下标b截取的值
// str.substr(a,b); //与前两个方法有区别
//获得结果为截取下标0起,由下标a起往后b位下标 console.log(test.slice(,-)); //o wo
console.log(test); //hello world
console.log(test.substr(,-)); // (空)
console.log(test); //hello world
console.log(test.substring(,-)); //hell
console.log(test); //hello world
//第2位传参为负整数
//1.都不会改变原有str的长度
//2.test.slice(a,b); 获得为下标0起,由下标a起往后b位下标截取(下标0起,即-3为下标4)
// test.substr(a,b); 返回为空
// test.substring(a,b); 获得为下标0起,由下标a起往前b位下标截取(下标0起,即-3为下标4)
最新文章
- Eclipse自动生成UML图(转载)
- chrome内核浏览器缓存资源找回方法
- Mysql数据库的索引原理
- C# Json处理日期和Table
- Objective-c 类接口 (@interface) (类定义)
- form验证及图片上传
- J.U.C并发框架
- bit 和 byte
- 如何在Ubuntu 16.04上安装PythonGDAL 2.1?
- pagerank 数学基础
- 软件安装配置笔记(三)——ArcGIS系列产品安装与配置(补档)(附数据库连接及数据导入)
- std::lock_guard/std::unique_lock
- 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容
- css position[转
- 修改oracle为归档模式
- Maven中groupId和artifactId的含义
- Cesium简介 [转]
- DataSet转换成List<;>;
- Java 处理cookie的方法
- 使用dockerfile 创建ubuntu ssh镜像