Javascript Substring

| | | |

The JavaScript substring () method retrieves a range of characters between the two index positions. You can use substring () to retrieve characters until the end of a string by omitting an end index position.

A substring is a smaller part of a larger string. Programmers use of sub - channels to extract the specific data they need in a string.

For example, you can divide the day, month, and year of a user’s birth date into three variables. This allows you to store each value on its own instead of storing the date of birth as a variable.

Or you can split a string to get the first two letters of a user’s name. In this guide, we will explore how you can use substring in JavaScript.

How to use the JavaScript substring method

The JavaScript substring () process extracts part of a string between two index values. Without a second value index, the sub - string () retrieves all characters after a number of particular index

The method of sub - string () takes two arguments:.

  • The position in which substring should start ; and
  • The position where the substring should Stop

Let’s look at the syntax of this method:.

The substring () is appended to the end of a string. Only the startup parameter is needed for the substring () method to work

JavaScript examples of sub - strings

Get characters at the end of ’a string

We have a string that contains the name of a cat. We want to remove the first two characters from the cat’s name and see what’s left. To do this we can use the substring () method:

In the first line of code, we define a variable called catName. We assign the value Pickles to this variable. Next, we define a variable JavaScript which gets a substring of the catName variable.

We used 2 as a parameter in the substring () method field. This means that the method returns each letter after the second index in the string. Here is the output for our example:

ckles

Now we can see all letters that appear after index position 2.

Get characters from range

h3>

If we are trying to get part of a string that starts with one character and ends with another, we should pass two values ‚Äã‚Äãinto > substring () The first argument is the start and the second is the end character.

We need the first three characters of our cat name:

The output for the example is as above:

Pic

Our string contains all the characters in the index range of 0 and 3. Remember , strings are indexed zero. This means that if you want to retrieve the first character of a string you have to start your substring at 0.

Retrieve the last character

We retrieve the last character of our chain. We cannot use negative index numbers with the substring method. Instead of this, we can use the length method to find the length of a string. Then we can use this number to retrieve the last character in our string:

The result is:

s

this example, catName.length gets the length of our string - the number of characters it has -. and returns the last character in the string

Conclusion

The method return substring JavaScript () characters between two index values ‚Äã‚Äãin the listing. If you don’t specify a final index value, substring () returns all characters after a given index position.

Developers use the substring () method to split a string into multiple parts. This is useful if a developer only needs one piece of info from a string

To learn more about coding in JavaScript, read our guide on best JavaScript beginner tutorials .