• JavaScript Video Tutorials

JavaScript String substr() Method



The JavaScript String substr() method is used to extract a portion (a part of the string) of a string starting at a specified index and extending for a given number of characters.

Here are some additional points about the substr() method as follows −

  • If the start index value is greater than the string length, an empty string will be returned.
  • If the start index value is less than zero (or negative), it starts counting from the end of the string.
  • If the start index parameter is omitted or undefined, it's treated as 0.
  • If the length parameter value is less than zero (or negative), an empty string is returned.
The JavaScript String substr() method is a deprecated method. This feature is no longer recommended. It may have already been removed from the relevant web standards.

Syntax

Following is the syntax of JavaScript String substr() method −

substr(start, length)

Parameters

This method accepts two parameters named 'start' and 'length', which are described below −

  • start − The index (position) of the first character to include in the returned substring.
  • length (optional) − The number of characters to be extracted.

Return value

This method returns a new string containing the portion (or part) of the given string.

Example 1

If the length parameter is omitted, it starts extracting at the specified start position and extending till the end of the string.

In the following program, we are using the JavaScript String substr() method to extract a part of the string from the given string "TutorialsPoint" starting at the specified start position 3.

<html>
<head>
<title>JavaScript String substr() Method</title>
</head>
<body>
<script>
   const str = "TutorialsPoint";
   document.write("Given string: ", str);
   const start = 3;
   document.write("<br>The start position: ", start);
   //using the substr() method
   var new_str = str.substr(start);
   document.write("<br>The new string: ", new_str);
</script>    
</body>
</html>

Output

The above program returns a new string as "orialsPoint".

Given string: TutorialsPoint
The start position: 3
The new string: orialsPoint

Example 2

If we pass both the start and length parameters to this method, it extracts characters starting from the specified start position and continuing for the given length of characters.

The following is another example of the JavaScript String substr() method. We use this method to retrieve a part of a string from the given string "Hello World" starting at the specified start position 4 and extending till the given length of characters 8.

<html>
<head>
<title>JavaScript String substr() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   document.write("Given string: ", str);
   const start = 4;
   const length = 8;
   document.write("<br>The start position: ", start);
   document.write("<br>The length of the charcters: ", length);
   //using the substr() method
   var new_str = str.substr(start, length);
   document.write("<br>The new string: ", new_str);
</script>    
</body>
</html>

Output

After executing the above program, it will return a new string as −

Given string: Hello World
The start position: 4
The length of the charcters: 8
The new string: o World

Example 3

If the start parameter value exceeds the length of the given string, it returns an empty string.

In this example, we use the JavaScript String substr() method to extract a part of the string from the given string "JavaScript" starting from the specified start position 15.

<html>
<head>
<title>JavaScript String substr() Method</title>
</head>
<body>
<script>
   const str = "JavaScript";
   document.write("Given string: ", str);
   const start = 15;
   document.write("<br>The start position: ", start);
   //using the substr() method
   var new_str = str.substr(start);
   document.write("<br>The new string: ", new_str);
</script>    
</body>
</html>

Output

Once the above program is executed, it will return an empty string.

Given string: JavaScript
The start position: 15
The new string:
Advertisements