Javascript substring method | Get Substring of given string example

Javascript substring Method is used to get the substring of the given String. Actually JS substring() method returns the part of the string between the start and end index.

Syntax

Parameters

  • indexStart (Start)Integer value will the first character to include in the returned substring.
  • indexEnd (End)– An optional integer value will the character to exclude from the returned substring.

Return value

It will return a new string containing the specified part of the given string.

Examples of Javascript substring method

The substring() method does not change the original given string.

Using Start and Stop indices

Begin the extraction at position 0, and extract up to 8 index of string:

Output:

Examples of Javascript substring method

Get only the first character

Output: E

Get only the last character:

Output: l

Q: What is Javascript substring indexof() method?

Answer: The indexOf() method returns the position of the first occurrence of a specified value in a string.

How to javascript substring after character?

Answer: You can use String.slice with String.lastIndexOf to get substring after the last specific character in JavaScript.

See below example:-

Q: How to javascript substring before character?

Answer: Get substring before any char you can use a substring() and indexof() method in JS.

See below example how to use it.

OR

Do comment if you have any doubts and suggestions on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.