在JS中,slice()、substring()、substr()都有截取字符串的作用,那他们有哪些用法上的区别呢?如果你也有疑惑,这篇文章或许能够帮助到你。

一、substring()

substring()方法返回一个索引和另一个索引之间的字符串,语法如下:

str.substring(indexStart, [indexEnd])

下面有六点需要注意:

  • substring()从提取的字符indexStart可达但不包括 indexEnd
  • 如果indexStart 等于indexEnd,substring()返回一个空字符串。
  • 如果indexEnd省略,则将substring()字符提取到字符串的末尾。
  • 如果任一参数小于0或是NaN,它被视为为0。
  • 如果任何一个参数都大于stringName.length,则被视为是stringName.length。
  • 如果indexStart大于indexEnd,那么效果substring()就好像这两个论点被交换了一样; 例如,str.substring(1, 0) == str.substring(0, 1)

以下是一些示例代码:

var str = 'abcdefghij';
console.log('(1, 2): ' + str.substring(1, 2)); // '(1, 2): b'
console.log('(1, 1): ' + str.substring(1, 1)); // '(1, 1): '
console.log('(-3, 2): ' + str.substring(-3, 2)); // '(-3, 2): ab'
console.log('(-3): ' + str.substring(-3)); // '(-3): abcdefghij'
console.log('(1): ' + str.substring(1)); // '(1): bcdefghij'
console.log('(-20, 2): ' + str.substring(-20, 2)); // '(-20, 2): ab'
console.log('(2, 20): ' + str.substring(2, 20)); // '(2, 20): cdefghij'
console.log('(20, 2): ' + str.substring(20, 2)); // '(20, 2): cdefghij'

二、substr()

substr()方法返回从指定位置开始的字符串中指定字符数的字符,语法如下:

str.substr(start, [length])

下面有四点需要注意:

  • substr()会从start获取长度为length字符(如果截取到字符串的末尾,则会停止截取)。
  • 如果start是正的并且大于或等于字符串的长度,则substr()返回一个空字符串。
  • start为负数,则将该值加上字符串长度后再进行计算(如果加上字符串的长度后还是负数,则从0开始截取)。
  • 如果length为0或为负数,substr()返回一个空字符串。如果length省略,则将substr()字符提取到字符串的末尾。

以下是一些示例代码:

var str = 'abcdefghij';
console.log('(1, 2): ' + str.substr(1, 2)); // '(1, 2): bc'
console.log('(-3, 2): ' + str.substr(-3, 2)); // '(-3, 2): hi'
console.log('(-3): ' + str.substr(-3)); // '(-3): hij'
console.log('(1): ' + str.substr(1)); // '(1): bcdefghij'
console.log('(-20, 2): ' + str.substr(-20, 2)); // '(-20, 2): ab'
console.log('(20, 2): ' + str.substr(20, 2)); // '(20, 2): '

需要注意的是,Microsoft的JScript不支持起始索引的负值。如果要使用此功能,可以使用以下兼容性代码来解决此错误:

// only run when the substr() function is broken
if ('ab'.substr(-1) != 'b') {
/**
* Get the substring of a string
* @param {integer} start where to start the substring
* @param {integer} length how many characters to return
* @return {string}
*/
String.prototype.substr = function(substr) {
return function(start, length) {
// call the original method
return substr.call(this,
// did we get a negative start, calculate how much it is from the beginning of the string
// adjust the start parameter for negative value
start < 0 ? this.length + start : start,
length)
}
}(String.prototype.substr);
}

三、substring()与substr()的主要区别

substring()方法的参数表示起始和结束索引,substr()方法的参数表示起始索引和要包含在生成的字符串中的字符的长度,示例如下:

var text = 'Mozilla';
console.log(text.substring(2,5)); // => "zil"
console.log(text.substr(2,3)); // => "zil"

四、slice()

slice()方法返回一个索引和另一个索引之间的字符串,语法如下:

str.slice(beginIndex[, endIndex])

下面有三点需要注意:

  • beginIndex为负数,则将该值加上字符串长度后再进行计算(如果加上字符串的长度后还是负数,则从0开始截取)。
  • 如果beginIndex大于或等于字符串的长度,则slice()返回一个空字符串。
  • 如果endIndex省略,则将slice()字符提取到字符串的末尾。如果为负,它被视为strLength + endIndex其中strLength是字符串的长度。

以下是一些示例代码:

var str = 'abcdefghij';
console.log('(1, 2): ' + str.slice(1, 2)); // '(1, 2): b'
console.log('(-3, 2): ' + str.slice(-3, 2)); // '(-3, 2): '
console.log('(-3, 9): ' + str.slice(-3, 9)); // '(-3, 9): hi'
console.log('(-3): ' + str.slice(-3)); // '(-3): hij'
console.log('(-3,-1): ' + str.slice(-3,-1)); // '(-3,-1): hi'
console.log('(0,-1): ' + str.slice(0,-1)); // '(0,-1): abcdefghi'
console.log('(1): ' + str.slice(1)); // '(1): bcdefghij'
console.log('(-20, 2): ' + str.slice(-20, 2)); // '(-20, 2): ab'
console.log('(20): ' + str.slice(20)); // '(20): '
console.log('(20, 2): ' + str.slice(20, 2)); // '(20, 2): '

参考文档:MDN web docs

原文地址:王玉略的个人网站

JS字符串截取函数slice(),substring(),substr()的区别的更多相关文章

  1. 字符串截取函数slice, substring, substr

    在日常项目需求中,常常会遇到需要截取字符串操作的工作,而ECMAScript为我们提供了原生的截取字符串的函数,而且提供了三个:slice, substring, substr.我们怎么判断在什么时候 ...

  2. js字符串截取函数slice()、substring()、substr()

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

  3. js 字符串截取函数substr,substring,slice之间的差异

    js 字符串的截取,主要有三个函数,一般使用三个函数:substr,substring,slice. 而这三个函数是不完全一样的,平时很难记住,在这里做下笔记,下次遇到的时候,直接从这里参考,调用合适 ...

  4. JavaScript中字符串截取函数slice()、substring()、substr()

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

  5. 关于js的string的3个函数slice,substring,substr对比

    slice,substring,substr三个函数都是截取字符串,但是对参数的处理有区别 参数处理相似的两个函数式slice和substring slice(start,end)和substring ...

  6. slice,substring,substr的区别

    1.都为正整数//例子数据 var arr = [1,2,3,4,5,6,7], var str = "helloworld!"; //注意这里有个!号也算一位若有空格,空格也算一 ...

  7. 字符串截取slice() substring() substr()的区别?

    获取子字符串 slice()        substr()    substring() 不会修改字符串本身,他们只是返回一个基本类型的字符串值 var str='abcdefghijklmn'; ...

  8. Mysql字符串截取函数SUBSTRING的用法说明

    感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取字符串 left(str, length) 说明:le ...

  9. js 字符串操作函数有哪些

    js 字符串操作函数有哪些 一.总结 一句话总结:js字符串函数都是字符串对象的方法,是通过调用字符串方法的方式调用,和java,php里面不一样. 1.字符串替换函数怎么用? 这里的正则表示是加双引 ...

随机推荐

  1. javascript 最全面的数组操作合集

    一.数组添加.删除.替换.截取操作 1.arr.unshift(1) 在数组头部添加一个元素 1 (直接改变原数组,返回值为添加元素后数组的length) 2.arr.shift() 在数组的头部删除 ...

  2. Java学习之Servlet篇

    <JAVA遇见HTML——Servlet篇> Servlet 生命周期:Servlet 加载--->实例化--->服务--->销毁. init():在Servlet的生命 ...

  3. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  4. spring的摘录

  5. 从零开始学 Web 之 移动Web(七)Bootstrap

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  6. MongoDB 系列文章

    MongoDB 系列文章 本文的内容是基于 MongoDB 4.0 的. 参考于 MongoDB 4.0 官方文档. 搭建 MongoDB从搭建到优化 MongoDB-副本集搭建与管理 管理 Mong ...

  7. orcal10g下载地址

    http://download.oracle.com/otn/nt/oracle10g/10201/102010_win64_x64_database.zip https://updates.orac ...

  8. sql server备份和还原

    官方文档:http://msdn.microsoft.com/zh-cn/library/ms187048%28v=sql.90%29.aspx

  9. thinkphp常用标签总结

    三元运算标签 {$v['member_id'] == 1 ? 正常 : 不正常} volist标签 <volist name="data" id="v"& ...

  10. 交叉编译问题记录-嵌入式环境下 GDB 的使用方法

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10693247.html 本文以嵌入式 Linux 环境下的 gdb 使用为例,记录交叉编 ...