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 程序设计与综合应用>张 ...
随机推荐
- 线段树-hdu2795 Billboard(贴海报)
hdu2795 Billboard 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子 思路:每次找到最大值的位子,然后减去L 线段树功能:query:区间求最大值的位子(直接 ...
- UVALive 6913 I Want That Cake 博弈+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/96343 I Want That Cake Time Limit: 3000MS 64bit IO Forma ...
- HDU 5672 String
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5672 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 不要USB数据线调试Android开发
不管是过去Eclipse还是现在的Android Studio开发Android,运行或者调试时都会利用USB数据线连接电脑和手机,特别是当现在的手机只有一个Type-c接口,意味着,插上后,啥也干不 ...
- Java NIO:IO与NIO的区别 -阿里面试题
一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...
- Spring mvc 数据验证框架注解
@AssertFalse 被注解的元素必须为false@AssertTrue 被注解的元素必须为false@DecimalMax(value) 被注解的元素必须为一个数字,其值必须小于等于指定的最小值 ...
- java垃圾回收 - 为什么要进行垃圾回收
1.为什么要进行垃圾回收: 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象 的内存时,该内存便 ...
- BZOJ2005 能量汇集 【gcd求和】
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MB Submit: 4368 Solved: 2607 [Submit][Sta ...
- scala(三)
一.面向对象编程——类 1.定义一个简单的类 class HelloWorld { private var name = "leo" def sayHello() { print( ...
- bzoj5118: Fib数列2(费马小定理+矩阵快速幂)
题目大意:求$fib(2^n)$ 就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有 注意因为模数比较大会爆LL,得写快速乘法... #include<bits/stdc++.h& ...