在 JavaScript 中,对于字符串的操作有 substring, substr, slice 等好多个内置函数,这里给大家推荐一篇介绍 substring, substr, slice 三者区别的文章。

  Extracting a portion of a string is a fairly well understood practice. With JavaScript, there are three different built-in functions which can perform that operation. Because of this, often it is very confusing for beginners as to which function should be used. Even worse, sometimes it is easy to fall into the trap and choose the wrong function.

  String’s substring (ECMAScript 5.1 Specification Section 15.5.4.15) is the first logical choice to retrieve a part of the string. This substring function can accept two numbers, the start and the end (exclusive) character position, respectively. In case the end value is smaller than the start, substring is smart enough toswap the values before doing the string extraction. An example of substring is illustrated in this code snippet:

var a = 'The Three Musketeers';
a.substring(4, 9); 'Three'
a.substring(9, 4); 'Three'

  Many JavaScript environments (including most modern web browsers) also implement a variant ofsubstring called substr (Section B.2.3). However, the parameters for substr are the startcharacter position and the numbers of characters to be extracted, respectively. This is shown in the following fragment:

var b = 'The Three Musketeers';
b.substr(4, 9); 'Three Mus'
b.substr(9, 4); ' Mus'

  This pair of functions, when they are both available, can be really confusing. It is so easy to mistake one for another and thereby leading to an unexpected outcome. It also does not help that the names,substring and substr, are too similar. Without looking at the documentation or the specification, there is a chance of picking a wrong one.

  To add more confusion to this mixture, a String object also supports slice (Section 15.5.4.13), just like in Array’s slice. For all intents and purposes, slice has a behavior very close to substring(accepting start and end position). However, there is a minor difference. If the end value is smaller than the start, slice will not internally swap the values. In other words, it follows what is expected for Array’s slice in the same situation and thus it returns an empty string instead.

var c = 'The Three Musketeers';
c.slice(4, 9); 'Three'
c.slice(9, 4); ''

  Each of these three function can accept two parameters and perform the string extraction based on those parameter values. The result however can be different. Again, it is just like in the confusing case of Array methods (see my previous blog post on JavaScript Array: slice vs splice)

  When we write our own JavaScript library, how can we minimize such a confusion? The solution is of course to avoid an API which leads to this situation at the first place. Whenever a new public function needs to be introduced, search for existing ones to ensure that there will not be a similar confusion. Of course, it is even better if such a step is enlisted in the API review checklist.

  Prevention is the best cure. Be advised of your function name!

  link: http://www.cnblogs.com/oooweb/p/javascript-string-substring-substr-slice.html

  via ofilabs

JavaScript 字符串操作:substring, substr, slice的更多相关文章

  1. JavaScript 字符串操作

    JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...

  2. JS substring substr slice区别

    1.api说明 (1)substring str.substring(indexStart[, indexEnd]) substring 提取从 indexStart 到 indexEnd(不包括)之 ...

  3. substring substr slice 区别

    1. substring(start,end)  返回指定索引区间的字串,不改变原字符串 start 必需,开始位置的索引,一个非负的整数 end  可选,结束位置的索引(不包括其本身),如果未设置, ...

  4. JS 中的substring ,substr ,slice,split,join

    substr with different arguments passed in: str.substring(startNum,stopNum ); str.slice(startNum,stop ...

  5. 截取字符串 substring substr slice

    截取字符串   substring 方法用于提取字符串中介于两个指定下标之间的字符  substring(start,end) 开始和结束的位置,从零开始的索引 参数     描述 start     ...

  6. js字符串操作之substr与substring

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

  7. JS 中substring() , substr(), slice() 的区别

    substr(start, length) : 截取从start索引开始的字符,长度为length的字符串 substring(start, end) : 截取从start索引开始的字符,以end索引 ...

  8. javaScript字符串操作

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...

  9. matlab学习笔记10_5 通用字符串操作和比较函数

    一起来学matlab-matlab学习笔记10 10_5 通用字符串操作和比较函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张 ...

随机推荐

  1. 《我是一只it小小鸟》观后感

    在这个学期开始的时候我们的老师推荐给我们这本书.在很多的网站上只要一提到IT,总会有人推荐这本书,我在读这本书之前看了很多关于它的书评,其中有一位网友的一句话让我对它产生了很大的兴趣:“印象最深的是书 ...

  2. mui.ajax与服务器(SpringMVC)传输json数据

    跨域问题 PC端为了安全,所以禁止跨域.而我使用mui做移动web时,难免会使用pc浏览器进行调试.mui.ajax是允许跨域的.为了可以调试成功,需要对浏览器进行设置及.以360急速浏览器为例,设置 ...

  3. Python——cmd调用(os.system阻塞处理)(多条命令执行)

    os.system(返回值为0,1,2)方法 0:成功 1:失败 2:错误 os.system默认阻塞当前程序执行,在cmd命令前加入start可不阻塞当前程序执行. 例如: import os os ...

  4. LR监控apache服务器

    开启mod_status模块功能,在LR的controller中找到apache资源图双击并右键添加度量,如下图:      添加apache服务器IP地址.选择系统平台.添加需要监控的计数器即可进行 ...

  5. 分页---Vue+.net+bootstrap实现

    通过学习Vue,的确觉的Vue的双向绑定使用起来十分方便,因此研究了一下列表显示时分页的实现,这里我使用了bootstrap的样式,所以在页面中引用bootstrap的样式文件,后台提数据源使用.ne ...

  6. wp如何代码重启手机

    用过windows phone手机操作系统的人都知道,wp的系统设置界面很长一串,我们并不能快速进入想要的设置项,更受不了的是有些常用的设置项竟然在最下边.因为前段时间没事做,于是乎写了个wp的工具类 ...

  7. Contest 9

    A:搜索好难啊根本不会啊. B:暴力枚举给哪段前缀乘,维护一些前后缀最大最小值之类的东西就很好算了. #include<iostream> #include<cstdio> # ...

  8. VSS2005设置不输入密码直接登录VSS

    1.登录管理员 2.Tools-->Options-->General -->Use network name for automatic user log in  去掉勾选不自动登 ...

  9. BeanUtils介绍及其使用

    BeanUtils工具由Apache软件基金组织编写,提供给我们使用,主要解决的问题是:把对象的属性数据封装到对象中.在整个J2EE的编程过程中,我们经常会从各种配置文件中读取相应的数据,需要明白的一 ...

  10. 【WebAPI】新手入门WebAPI

    一.前言       工作也有一年多了,从进入公司就一直进行BIM(建筑信息模型)C/S产品的研发,平时写的最多的就是Dev WPF.一个偶然的时机,产品需要做支付宝扫码与微信扫码,所以需要了解产品服 ...