JavaScript

How to Extract String Parts in JavaScript

JavaScript is a scripting language that is mostly used for web development. JavaScript comes bundled with many distinguishing features. Extracting string parts is one of its many noticeable features. Javascript provides two methods to extract string parts that are given below. Each of these methods performs a different type of string extraction.

  1. substr ( ) method
  2. substring ( ) method

Let’s discuss each of these methods in detail.

substr() Method

For the purpose of extracting a part of the string in JavaScript, the substr() method is used. This method will extract parts of the string from a specified position. As a result, only a specific amount of desired characters will be extracted without altering the original string.

Syntax of substr() method

The syntax of extracting a part of a string in JavaScript is as follows:

string.substr(start, length) 

In the syntax, the parameter start refers to the position from where you want to extract the string and length refers to the number of characters you want to extract.

For better understandability see the following examples.

Example 1

Suppose.

let text= "I want to extract a string part";

Let’s say we want to extract 5 letters from the first index. For doing so, the substr() method arguments would go like this:

let result = text.substr(1,5);

The extracted string will be stored in the “result” variable. To verify, let’s log the “result” variable on the browser’s console:

console.log(result);


As you can verify by looking at the output screenshot attached above that we have got our desired substring from the original string.

Example 2

Now suppose you want to extract a different number of characters from a different position then you simply have to change the parameters.

let text= "I want to extract a string part";

Suppose you want to extract 3 characters only this time. The substr ( ) arguments would go like this.

let result= text.substr(3,3);

For extracting the result we would do this.

console.log(result);


The screenshot above shows the relevant code along with the output.

Example 3

In order to extract parts of a string from the end position we use a negative value for the start position. Here is how you do it.

let text= "I want to extract a string part";let result= text.substr(-2,5);

console.log(result);

The output is shown in the following screenshot.


Now we will discuss the next string extraction method.

substring() Method

There is another method in JavaScript for extracting string parts which takes the starting and ending index for extracting part of a string known as substring() method.

Syntax of substring( ) method

Following is the syntax of the aforementioned method.

string.substring(start, end)

By determining the syntax of the substring( ) method we can pinpoint the major dissimilarity between the substr( ) and substring( ) methods. The substr ( ) is used to extract a specific number of characters from a specified position while this substring() method is used to extract characters lying between the starting and ending positions of a string.

Let us demonstrate a substring( ) method example.

Example:

Suppose we want to extract the letters from first index to the fifth index. For extracting such a result, the arguments of substring() method would be (1,5) as shown in the javascript code snippet below:

let text= "I want to extract a string part";
let result= text.substring(1,5);
console.log(result);

In the following screenshot the output is demonstrated.


A few things that should be kept in mind while using the substring( ) method are as follows:

  1. Zero and negative values for start and end arguments are considered zero.
  1. If the value for the start parameter is more than the value of the end parameter then the parameters are swapped. For instance (5,1) will be swapped to (1,5).

Conclusion

JavaScript provides its users with two easy-to-use methods of extracting parts of a string. First, the substr( ) method allows you to extract a specific number of characters from a particular position in a string, and the substring( ) method is used to extract characters lying in between the start and end indices of a string. In this tutorial, the usage of both of these methods is shown along with appropriate examples.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.