Languages
[Edit]
EN

JavaScript - string substring method example

14 points
Created by:
Marcin
18610

In this article, we're going to have a look at different ways to use String.prototype.substring method in JavaScript.

Quick overlook:

// ONLINE-RUNNER:browser;

//   index: 012
var text = "abc";

console.log( text.substring(0) ); // abc
console.log( text.substring(1) ); // bc
console.log( text.substring(2) ); // c
console.log( text.substring(3) ); //

console.log( text.substring(0, 0) ); //
console.log( text.substring(0, 1) ); // a
console.log( text.substring(0, 2) ); // ab
console.log( text.substring(0, 3) ); // abc

Note:

More detailed and grouped substring method description is placed below the documentation.

 

1. Documentation

SyntaxString.prototype.substring(indexStart[, indexEnd])
Parameters

indexStart - integer between 0 and a number one less than the string length.

indexEnd (optional) - integer between 0 and the length of the string.

ResultA new string containing the specified part of the given string.
DescriptionString substring returns the part of the string between the indexStart and indexEnd, or from indexStart to the end of the string.

2. String substring with one agrument example

Example presented this section shows how to get substring starting from index passed as first argument to end of string.

// ONLINE-RUNNER:browser;

//   index: 012
var text = "abc";

console.log( text.substring(0) ); // abc
console.log( text.substring(1) ); // bc
console.log( text.substring(2) ); // c
console.log( text.substring(3) ); //

3. String substring with two agruments example

Example presented this section shows how to get substring starting from index passed as first argument to the ending index passed as second argument.

// ONLINE-RUNNER:browser;

//   index: 012
var text = "abc";

console.log( text.substring(0, 0) ); //
console.log( text.substring(0, 1) ); // a
console.log( text.substring(0, 2) ); // ab
console.log( text.substring(0, 3) ); // abc

console.log( text.substring(1, 1) ); // 
console.log( text.substring(1, 2) ); // b
console.log( text.substring(1, 3) ); // bc

console.log( text.substring(0, 3) ); // abc
console.log( text.substring(2, 3) ); // c
console.log( text.substring(3, 3) ); //

console.log( text.substring(0, text.length    ) ); // abc
console.log( text.substring(0, text.length - 1) ); // ab
console.log( text.substring(0, text.length - 2) ); // a

4. String substring with swapped arguments example

In this case, the substring behaves similar to second example. One difference is if first argument is bigger than the second one then they are swapped.

// ONLINE-RUNNER:browser;

//   index: 012
var text = "abc";

console.log( text.substring(3, 0) ); // abc
console.log( text.substring(2, 0) ); // ab
console.log( text.substring(1, 0) ); // a

5. String substring with negative arguments example

When arguments are negative then they are converted to 0.

// ONLINE-RUNNER:browser;

//   index: 012
var text = "abc";

console.log( text.substring(-1, 3) ); // abc
console.log( text.substring(-2, 3) ); // abc
console.log( text.substring(-3, 3) ); // abc

console.log( text.substring(-1, -3) ); // 

JavaScript - String

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join