首先,他们都接收两个参数,slice和substring接收的是起始位置和结束位置(不包括结束位置),而substr接收的则是起始位置和所要返回的字符串长度。直接看下面例子:

var test = 'hello world';

alert(test.slice(4,7));             //o w
alert(test.substring(4,7)); //o w
alert(test.substr(4,7)); //o world

这里有个需要注意的地方就是:substring是以两个参数中较小一个作为起始位置,较大的参数作为结束位置。

如:

alert(test.substring(7,4));          //o w

接着,当接收的参数是负数时,slice会将它字符串的长度与对应的负数相加,结果作为参数;substr则仅仅是将第一个参数与字符串长度相加后的结果作为第一个参数;substring则干脆将负参数都直接转换为0。测试代码如下:

var test = 'hello world';

alert(test.slice(-3));         //rld
alert(test.substring(-3)); //hello world
alert(test.substr(-3)); //rld alert(test.slice(3,-4)); //lo w
alert(test.substring(3,-4)); //hel
alert(test.substr(3,-4) //空字符串

注意:IE对substr接收负值的处理有错,它会返回原始字符串。

slice,substr和substring的区别的更多相关文章

  1. js里slice,substr和substring的区别

    概要: string.slice(start, end)提取一个字符串 string.substring(start, end)提取一个字符串,end不支持负数 string.substr(start ...

  2. 【转】slice,substr和substring的区别

    首先,他们都接收两个参数,slice和substring接收的是起始位置和结束位置(不包括结束位置),而substr接收的则是起始位置和所要返回的字符串长度.直接看下面例子: var test = ' ...

  3. 字符串和数组中split().toString(),join(),splice(),slice(),substr()和substring()

    <!Doctype html> <head> <mate charset="utf-8"> <title>string change ...

  4. 字符串截取 及 substr 和 substring 的区别

    1..字符串截取 str.substr(0, 1) // 获取字符串第一个字符 str.substr(-1) // 获取字符串最后一个字符 str.charAt(str.length - 1) // ...

  5. 字符串截取 slice,substr,substring 的区别

    一 只传递一个参数时候 let str = '0123456'; str.slice(5); //'56' str.substr(5); // '56' str.substring(5); // '5 ...

  6. 如何使用slice,substr代替substring(原创)

    //写这个是为了加深对substring和slice的理解 substring: 任何一个参数小于0,都会被替换成0.两个参数,最小值会被当做start,最大值当做end. 参数 描述 start 必 ...

  7. substr与substring的区别

    在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与区别吧 ...

  8. Javascript中substr和substring的区别

    由于在项目中有需要对字符串进行截取,然后手残使用了IDE自动提示的substr,没想那么多以为substr和substring没多大区别. 然而并不是,且听我一一道来. 1. substr(index ...

  9. substr和substring的区别

    substr和substring两个都是截取字符串的. 两者有相同点,如果只是写一个参数,两者的作用都是一样的:就是截取字符串当前下标以后直到字符串最后的字符串片段. 例如:`var a=”abcde ...

随机推荐

  1. pod install 和 pod update的区别

    pod install 和 pod update的区别 pod install(下载并安装pod) 1,当pod file文件中有“增加pod,删除pod,修改pod”的操作之后使用. 2,pod i ...

  2. 【代码笔记】iOS-单项选择框

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  3. 关于学习YYKit的记录

    <1>遇到的问题 <1>使用@[].mutableCopy创建可变数组 代码出处:YYKitDemo-> YYRootViewController 源代码:self.ti ...

  4. C#复习⑦

    C#复习⑦ 2016年6月22日 11:50 Main Exception & Namespaces & Assemblies 异常 & 命名空间 & 程序集 1.tr ...

  5. git diff的用法

    在git提交环节,存在三大部分:working tree(工作区), index file(暂存区:stage), commit(分支:master) working tree:就是你所工作在的目录, ...

  6. git浅谈

    我们为什么要使用git 应用场景分析 1.使用svn,已经开发完一个需求,正在开发第二个需求,但是测试需要你立刻将你完成的第一个需求提交,请问现在你该怎么做: svn的解决方法大概是这样的:打开提交视 ...

  7. [Linux 性能检测工具]IOSTAT

    IOSTAT NAME:          Iostat, 报告CPU的统计,和 I/O的统计. 语法: iostat  [ -c ] [ -d ] [ -N ] [ -n ] [ -h ] [ -k ...

  8. SQL Server调优系列进阶篇(查询语句运行几个指标值监测)

    前言 上一篇我们分析了查询优化器的工作方式,其中包括:查询优化器的详细运行步骤.筛选条件分析.索引项优化等信息. 本篇我们分析在我们运行的过程中几个关键指标值的检测. 通过这些指标值来分析语句的运行问 ...

  9. 像编程一样写文章—Markdown

    Markdown是什么 是一种极其简单的标记语言,写的时候只需要普通编辑器即可: 它可以使文本内存具有某种格式: Markdown设计理念使文本易读.易写 文件后缀名:.md . .markdown. ...

  10. 烂泥:openvpn双网卡客户端与内网机器通信

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 前段时间写了一篇有关openvpn搭建与内网机器通信的文章,那篇文章是基于服务器单网卡 ...