非常教程

JavaScript参考手册

字符串 | String

string.substring

substring() 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。

语法

str.substring(indexStart, [indexEnd])

参数

indexStart一个 0 到字符串长度之间的整数。

indexEnd可选。一个 0 到字符串长度之间的整数。

返回值

包含给定字符串的提取部分的新字符串。

描述

substring提取从 indexStart 到indexEnd(不包括)之间的字符。特别地:

  • 如果 indexStart 等于 indexEndsubstring 返回一个空字符串。

  • 如果省略 indexEndsubstring 提取字符一直到字符串末尾。

  • 如果任一参数小于 0 或为 NaN,则被当作 0。

  • 如果任一参数大于 stringName.length,则被当作 stringName.length

如果 indexStart 大于 indexEnd,则 substring 的执行效果就像两个参数调换了一样。例如,str.substring(1, 0) == str.substring(0, 1)

示例

例子:使用 substring

下例使用 substring 输出字符串 "Mozilla" 中的字符:

var anyString = 'Mozilla';

// Displays 'Moz'
console.log(anyString.substring(0, 3));
console.log(anyString.substring(3, 0));

// Displays 'lla'
console.log(anyString.substring(4, 7));
console.log(anyString.substring(4));
console.log(anyString.substring(7, 4));

// Displays 'Mozill'
console.log(anyString.substring(0, 6));

// Displays 'Mozilla'
console.log(anyString.substring(0, 7));
console.log(anyString.substring(0, 10));

运用 length 属性来使用 substring()

下面一个例子运用了    String.length 属性去获取指定字符串的倒数元素。显然这个办法更容易记住,因为你不再像上面那个例子那样去记住起始位置和最终位置。

// Displays 'illa' the last 4 characters
var anyString = 'Mozilla';
var anyString4 = anyString.substring(anyString.length - 4);
console.log(anyString4);

// Displays 'zilla' the last 5 characters
var anyString = 'Mozilla';
var anyString5 = anyString.substring(anyString.length - 5);
console.log(anyString5);

substring()substr()之间的区别

方法substring()substr()方法之间有一个微妙的区别,你应该小心不要让他们困惑。

如果substring()方法的参数表示起始索引和结束索引,则substr()方法参数表示要包含在结果字符串中的起始索引和字符长度。

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

例子:替换一个字符串的子字符串

下例替换了一个字符串中的子字符串。可以替换单个字符和子字符串。该例结尾调用的函数将 "Brave New World" 变成了 "Brave New Web"。

// Replaces oldS with newS in the string fullS
function replaceString(oldS, newS, fullS) {
  for (var i = 0; i < fullS.length; ++i) {
    if (fullS.substring(i, i + oldS.length) == oldS) {
      fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
    }
  }
  return fullS;
}

replaceString('World', 'Web', 'Brave New World');

请注意,如果oldS本身是一个子字符串,则可能会导致无限循环newS- 例如,如果您试图在此处用“OtherWorld”替换“World”。一个更好的替换字符串的方法如下:

function replaceString(oldS, newS, fullS) {
  return fullS.split(oldS).join(newS);
}

上面的代码是子串操作的一个例子。如果您需要替换子字符串,大部分时间您将要使用String.prototype.replace()

规范

Specification

Status

Comment

ECMAScript 1st Edition (ECMA-262)

Standard

Implemented in JavaScript 1.0.

ECMAScript 5.1 (ECMA-262)The definition of 'String.prototype.substring' in that specification.

Standard

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'String.prototype.substring' in that specification.

Standard

ECMAScript Latest Draft (ECMA-262)The definition of 'String.prototype.substring' in that specification.

Living Standard

浏览器兼容性

Feature

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络