Languages
[Edit]
EN

JavaScript - string substring method example

14 points
Created by:
Marcin
2462

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

 

More detailed and grouped substring method description is placed below.

1. 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) ); //

2. 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

3. String substring with swaped agruments example

In this case substring behaves similar to second example. One difference is if first argument is bigger than second one than 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

4. 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) ); // 

 

Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.