Extract Substring from a String in JavaScript

JavaScript extract substring from a string; Through this tutorial, i am going to show you how to extract substring from string in javaScript without using regex.

And as well as, how to extract a substring from a string with using start and end index in javascript.

JavaScript Extract Substring from a String

You can use string.substring() method to extract the substring from a string in javaScript.

Note that:- The JavaScript string.substring() method returns the new sub string from a given string, the basis of the provided index.

Syntax of JavaScript substring() method

See the following syntax of javaScript substring() method; which is used to extract substring from a string:

str.substring(indexA [, indexB])

Parameters of javaScriptstring.substring() 

This method takes two parameters: indexX and indexY:

  • The indexX is a position where to start the extraction.
  • The indexY  is a position where to end the extraction.

Examples 1 – Extracting a substring from the starting of the string

Take an example for extract a substring from the between two indexes of given string using substring() method; as shown below:

let string = 'Hello, Programmer';
let substring = string.substring(0,4);
console.log(substring);

Output:

Hello

Example 2 – Extract a substring from the end of a string

Take an simple example for extract a substring from the end of a string in javaScript; as shown below:

let string = 'Hello, Programmer';
let substring = string.substring(6);
console.log(substring);

Output:

Programmer

Conclusion

Extract a substring from a string in javascript; Through this tutorial, you have learned how to extract a substring from a string in javascript using string.substring() method.

More JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *