JavaScript 字符串操作:substring, substr, slice
在 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的更多相关文章
- JavaScript 字符串操作
JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...
- JS substring substr slice区别
1.api说明 (1)substring str.substring(indexStart[, indexEnd]) substring 提取从 indexStart 到 indexEnd(不包括)之 ...
- substring substr slice 区别
1. substring(start,end) 返回指定索引区间的字串,不改变原字符串 start 必需,开始位置的索引,一个非负的整数 end 可选,结束位置的索引(不包括其本身),如果未设置, ...
- JS 中的substring ,substr ,slice,split,join
substr with different arguments passed in: str.substring(startNum,stopNum ); str.slice(startNum,stop ...
- 截取字符串 substring substr slice
截取字符串 substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 参数 描述 start ...
- js字符串操作之substr与substring
substr和substring两个都是截取字符串的. 两者有相同点,如果只是写一个参数,两者的作用都是一样的:就是截取字符串当前下标以后直到字符串最后的字符串片段. 例如: `var a=" ...
- JS 中substring() , substr(), slice() 的区别
substr(start, length) : 截取从start索引开始的字符,长度为length的字符串 substring(start, end) : 截取从start索引开始的字符,以end索引 ...
- javaScript字符串操作
JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...
- matlab学习笔记10_5 通用字符串操作和比较函数
一起来学matlab-matlab学习笔记10 10_5 通用字符串操作和比较函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张 ...
随机推荐
- Alpha 冲刺6
队名:日不落战队 安琪(队长) 今天完成的任务 回收站前端界面. 明天的计划 查看个人信息界面. 还剩下的任务 信息修改前端界面. 设置界面. 遇到的困难 模拟机莫名其妙就崩了,调试了很久,后在队友的 ...
- lintcode-512-解码方法
512-解码方法 有一个消息包含A-Z通过以下规则编码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 现在给你一个加密过后的消息,问有几种解码的方式 样例 给你的消 ...
- Websphere Application Server 环境配置与应用部署最佳实践
在发布一个运行于 WebSphere Application Server 的 J2EE 应用之前,对服务器进行配置和部署应用是必不可少的一个过程,这个过程是非常复杂的.WAS 为用户提供了可视化的管 ...
- Servlet处理表单
- B-2阶段组员分数分配
组名: 新蜂 组长: 武志远 组员: 宫成荣 谢孝淼 杨柳 李峤 项目名称: java俄罗斯方块 武 武 武 武 杨 宫 宫 杨 宫 谢 李 杨 李 谢 李 谢 李 谢 杨 宫 扬 谢 宫 李 武 评 ...
- 设计模式php篇(一)————单例模式
话不多说,直接上代码: <?php namespace DesignPattern; /** * php设计模式之单例模式 */ class SingleInstance { private s ...
- java 基础 --Collection(Map)
Map是不是集合?哈哈哈 java编程思想>的第11章,第216页,正数第13行,中原文:“……其中基本的类型是LIst.Set.Queue和Map.这些对象类型也称为集合类,但由于Java类库 ...
- TClientDataSet[5]: 读取数据
本例用到: TClientDataSet.Fields[]; { 字段集合; 它比 FieldList 有更多功能, 如可获取嵌套字段 } TClientDataSet.FieldL ...
- ZOJ3529_A Game Between Alice and Bob
题目的意思是给你若干个数字,两个游戏者轮流操作,每次可以将该数变为一个小于当前的一个约数,无法操作的游戏者fail. 和其他的博弈题目大同小异吧. 不同点有两个,逐一分析吧. 一.每次改变一个数只能改 ...
- 深入理解JVM一性能监控工具
一.前言 工欲善其事必先利其器,性能优化和故障排查在我们大都数人眼里是件比较棘手的事情,一是需要具备一定的原理知识作为基础,二是需要掌握排查问题和解决问题的流程.方法.本文就将介绍利用性能监控工具,帮 ...