totn JavaScript

JavaScript: String substr() method

This JavaScript tutorial explains how to use the string method called substr() with syntax and examples.

Description

In JavaScript, substr() is a string method that is used to extract a substring from a string, given a start position and a length. Because the substr() method is a method of the String object, it must be invoked through a particular instance of the String class.

WARNING: It is recommended that you use the substring() method instead, if possible. The substr() method is a legacy function.

Syntax

In JavaScript, the syntax for the substr() method is:

string.substr(start_position [, length]);

Parameters or Arguments

start_position
The position in string where the extraction will start. The first position in string is 0. If a negative value is provided for this parameter, the substr() method will measure the position from the end of the string. For example, a value of -1 is the last character in the string, a value of -2 is the second last character in the string and so on.
length
Optional. It is the number of characters to extract from string. If this parameter is not provided, the substr() method will extract to the end of the string.

Returns

The substr() method returns a substring that has been extracted from string given the start_position and length to extract.

Note

  • The substr() method does not change the value of the original string.
  • See also the slice() and the substring() methods.

Example

Let's take a look at an example of how to use the substr() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.substr(0,4));
console.log(totn_string.substr(4,2));
console.log(totn_string.substr(6,3));
console.log(totn_string.substr(9,3));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the substr() method of the totn_string variable to extract the substring from the string.

We have written the output of the substr() method to the web browser console log, for demonstration purposes, to show what the substr() method returns.

The following will be output to the web browser console log:

Tech
On
The
Net

As you can see, the substr() method returned 'Tech' for the first call, 'On' for the second call, 'The' for the third call and 'Net' for the fourth call.

Specifying a Negative Start Position

You can also use a negative start_position to extract a substring using the substr() method. A negative start_position allows you to determine a position relative to the end of the string when extracting the substring.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.substr(-3));

The following will be output to the web browser console log:

Net

In this example, we have set the start_position parameter to a value of -3 and we have not provided a length parameter. This means that the substr() method will extract the substring starting at the third last character until the end of the string (allowing us to extract the last 3 characters of the string).