JS中string方法中常用方法之六:String.prototype.substring()
来源:互联网 发布:魅族手机备份数据 编辑:程序博客网 时间:2024/06/05 19:10
String.prototype.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 anyString = "Mozilla";
// 输出 "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));
console.log(anyString.substring(3,-3));
console.log(anyString.substring(3,NaN));
console.log(anyString.substring(-2,3));
console.log(anyString.substring(NaN,3));
// 输出 ""
console.log(anyString.substring(4,4));
// 输出 "zill"
console.log(anyString.substring(2));
// 输出 "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(-2,7));
console.log(anyString.substring(NaN,7));
console.log(anyString.substring(0,10));
语法: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 anyString = "Mozilla";
// 输出 "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));
console.log(anyString.substring(3,-3));
console.log(anyString.substring(3,NaN));
console.log(anyString.substring(-2,3));
console.log(anyString.substring(NaN,3));
// 输出 ""
console.log(anyString.substring(4,4));
// 输出 "zill"
console.log(anyString.substring(2));
// 输出 "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(-2,7));
console.log(anyString.substring(NaN,7));
console.log(anyString.substring(0,10));
1 0
- JS中string方法中常用方法之六:String.prototype.substring()
- JS中string方法中常用方法之十二:String.prototype.toLowerCase()和String.prototype.toUpperCase()
- JS中string方法中常用方法之二:String.prototype.concat()
- JS中string方法中常用方法之三:String.prototype.charCodeAt()
- JS中string方法中常用方法之五:String.prototype.slice()
- JS中string方法中常用方法之七:String.prototype.search()
- JS中string方法中常用方法之八:String.prototype.indexOf()
- JS中string方法中常用方法之九:String.prototype.match()
- JS中string方法中常用方法之十:String.prototype.toLocaleLowerCase()
- JS中string方法中常用方法之十一:String.prototype.toLocaleUpperCase()
- JS中string方法中常用方法之十三:String.prototype.replace()
- JS中string方法中常用方法之一:String.prototype.charAt()
- JS中string方法中常用方法之四:String.fromCharCode()
- js中string对象的常用方法
- JS中String对象常用的方法
- Java中String方法substring讲解
- String中substring方法内存泄漏问题
- String中substring方法内存泄漏问题
- js--eval
- CodeForces 584D Dima and Lisa
- Android新的加载模式-Loader
- selenium官网打不开怎么办呢
- udhcp源码详解(五)
- JS中string方法中常用方法之六:String.prototype.substring()
- hdu 2566
- Appium-doctor不是内部命令
- 树状数组_应用的代码
- 重建Actity
- Java struts 2 源码阅读入门
- linux环境下安装wireshark
- ACM输入五种形式
- 玲珑oj 1032A-B(组合数学)