• JavaScript Video Tutorials

JavaScript String substring() Method



The JavaScript String substring() method is used to retrieve a part of a string from the original string starting from the specified startIndex up to indexEnd(excluding this), and if the indexEnd is not specified, it extracted to the end of the string.

If the indexStart value is greater than the indexEnd parameter value, then the arguments will be swapped. For example, if indexStart = 5, and indexEnd = 2, then (5, 2) is equivalent to (2, 5). If either indexStart or indexEnd is less than zero, it is treated as 0.

Syntax

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

substring(indexStart, indexEnd)

Parameters

This method accepts two parameters: 'indexStart' and 'indexEnd', which are described below −

  • indexStart − The starting position from where string start extracted.
  • indexEnd − The end position from where up to the string will be extracted excluding this.

Return value

This method returns a new string from the original string between two indices.

Example 1

If we omit the indexEnd parameter, then the sustring() method extracts a substring starting from the specified indexStart position to the end of the string.

In the following program, we are using the JavaScript String substring() method to extract a part of the string from the original string "Tutorials Point". The extraction starts from the specified indexStart 10 and continues till the end of the string.

<html>
<head>
<title>JavaScript String substring() Method</title>
</head>
<body>
<script>
   const str = "Tutorials Point";
   let indexStart = 10;
   document.write("Original string: ", str);
   document.write("<br>Start index: ", indexStart);
   document.write("<br>Extracted string: ", str.substring(indexStart));
</script>    
</body>
</html>

Output

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

Original string: Tutorials Point
Start index: 10
Extracted string: Point

Example 2

If we pass both indexStart and indexEnd parameters to this method, it extracts the string starting from the indexStart and up to indexEnd(excluding this).

Here is another example of the JavaScript substring() method. We use this method to extract a part of the string from the string "Hypertext Markup Language" between the indices 5 and 15.

<html>
<head>
<title>JavaScript String substring() Method</title>
</head>
<body>
<script>
   const str = "Hypertext Markup Language";
   let indexStart = 5;
   let indexEnd = 16;
   document.write("Original string: ", str);
   document.write("<br>Indices are: ", indexStart, ", ", indexEnd);
   document.write("<br>Extracted string: ", str.substring(indexStart, indexEnd));
</script>    
</body>
</html>

Output

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

Original string: Hypertext Markup Language
Indices are: 5, 16
Extracted string: text Markup

Example 3

If the indexStart value is greater than the indexEnd parameter value, then the argument will be swapped.

As discussed, if indexStart is greater than indexEnd, then the arguments will be swapped so that the indices (5, 2) will be swapped and equal to the indices (2, 5).

<html>
<head>
<title>JavaScript String substring() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   let indexStart = 5;
   let indexEnd = 2;
   document.write("Original string: ", str);
   document.write("<br>Indices are: ", indexStart, ", ", indexEnd);
   document.write("<br>Extracted string(2, 5): ", str.substring(indexStart, indexEnd));
</script>    
</body>
</html>

Output

The above program returns a new substring "llo".

Original string: Hello World
Indices are: 5, 2
Extracted string(2, 5): llo
Advertisements