[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/substring [Back]  [Original]

String.prototype.substring() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.substring()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

substring() ( ), , , .

In this article

str.substring(indexA[, indexB])

indexA

0 , , .

indexB

. 0 , , .

substring() , indexA , , indexB. :

  • indexA indexB, substring() .
  • indexB , substring() .
  • , NaN, 0.
  • , stringName.length, stringName.length.

indexA indexB, substring() , ; , str.substring(1, 0) == str.substring(0, 1).

: substring()

substring() 'Mozilla':

js
var anyString = "Mozilla";

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

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

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

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

: substring() length

substring() length . , , , .

js
//  'illa' -  4 
var anyString = "Mozilla";
var anyString4 = anyString.substring(anyString.length - 4);
console.log(anyString4);

//  'zilla' -  5 
var anyString = "Mozilla";
var anyString5 = anyString.substring(anyString.length - 5);
console.log(anyString5);

:

. , . ' ' ' '.

js
//  oldS  newS   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("", "", "  ");

, , oldS newS , . :

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

. , String.prototype.replace().

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.substring


Web Proxy Viewer  |  New URL  |  Original Page