×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

JavaScript String substr() Method with Example

JavaScript String substr() Method: Here, we are going to learn about the substr() method in JavaScript with Example.
Submitted by IncludeHelp, on February 06, 2019

String substr() Method

substr() method is a string method in JavaScript, it is used to extract a part of the string (substring) from the given string.

Syntax

    String.substr(start, [length]);

Parameters

  • String is the main string.
  • start is the initial index from where we have to extract the part of the string.
  • length is an optional parameter, it defines the total number of characters to be extracted from the given index.

Sample Input/Output

    str = "IncludeHelp is made for students.";
    start = 10
    length = None 
    Output:
    "p is made for students."

    Input:
    str = "IncludeHelp is made for students.";
    start = 0
    length = 11
    Output:
    "IncludeHelp"

Example

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
<script>
	var str = "IncludeHelp is made for students.";
	var substring ="";
	
	substring = str.substr(10);
	document.write(substring + "<br>");

	substring = str.substr(0,11);
	document.write(substring + "<br>");	

	substring = str.substr(12,7);
	document.write(substring + "<br>");		
</script>
</body>
</html>

Output

p is made for students.
IncludeHelp
is made

JavaScript String Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.