Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ‚̧ ūüíĽ

If you think sharing knowledge and helping other is valuable.

join our community - Click here

JavaScript - string substring method example

0 contributions
14 points

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

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey ūüĎč
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more