Home » JavaScript

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]);

Here,

  • 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.

Examples:

    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"

Code:

<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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.