• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JScript » String » JavaScript String substr() Method
Next →
← Prev

JavaScript String substr() Method

By Dinesh Thakur

JavaScript Substr Method: If you wanted to pulls out a portion of a string and assign that cut-out part to another variable or use it in an expression, you would use the JavaScript Substr Method.

 Syntax of JavaScript String substr() Method:

string.substr(start, length) 

The first parameter start is the starting index and the second parameter is the length of the substring. Since you’re string can’t have a negative length, you can’t use a negative number for the second parameter (you can use a negative number for the first parameter, though). Of course, that second parameter is optional, if you want the rest of the string.

JavaScript Substr Method differs from String JavaScript substring() Method in two basic ways. One, in substring() the start position cannot be negative, that is, it must be 0 or greater. Two, the second parameter in substring() indicates a position to go to, not the length of the new substring.

<html> 
  <head> 
  <title>Example of JavaScript Substr Method</title>     
 </head>     
<body> 
     <font face="arial" size="+1">     Example of  Substr()     <font size="-1"> 
      <script language="JavaScript"> 
  var straddr = "ecomputernotes@gmail.com"; 
  document.write("<br>His name is<em> " + straddr.substr(0,14) + "</em>.<br>"); 
  var namesarr = straddr.split("@" ); 
  document.write( "The user name is<em> " +  namesarr[0] + "</em>.<br>"); 
  document.write( "and the mail server is<em> " +  namesarr[1] + "</em>.<br>"); 
  document.write( "The first character in the string is <em>" + straddr.charAt(0)+ "</em>.<br>"); 
  document.write( "and the last character in the string is <em>" + straddr.charAt(straddr.length - 1) + "</em>.<br>");     
</script>     
</body> 
</html>

EXPLANATION

1. A string is assigned an e-mail address.

2. The JavaScript Substr Method starts at the first character at position 0, and yanks 14 characters from the starting position. The substring is ecomputernotes.

3. The split() method creates an array, called namesarr, by splitting up a string into substrings based on some delimiter that marks where the string is split. This string is split using the @ sign as its delimiter.

4. The first element of the array, namesarr[0], that is created by the split() method is ecomputernotes, the user name portion of the e-mail address.

5. The second element of the array, namesarr[1], that is created by the split() method is gmail.com, the mail server and domain portion of the e-mail address.

6. The charAt() method returns the character found at a specified position within a string; in this example, position 0. Position 0 is the first character in the string, a letter e.

7. By giving the charAt() method the length of the string minus 1, the last character in the string is extracted, a letter m.

 

You’ll also like:

  1. JavaScript String substring() Method
  2. JavaScript String split() Method
  3. JavaScript String search() Method
  4. JavaScript String match() Method
  5. JavaScript String replace() Method
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

JavaScript Tutorials

JavaScript Tutorials

  • JavaScript - Home
  • JavaScript - Operators
  • JavaScript - Comments
  • JavaScript - If...Else
  • JavaScript - For Loop
  • JavaScript - Popup Boxes
  • JavaScript - Switch Statement
  • JavaScript - while Loop
  • JavaScript - Functions
  • JavaScript - Variables
  • JavaScript - Statements
  • JavaScript - Substring()
  • JavaScript - indexOf()
  • JavaScript - Match()
  • JavaScript - Replace()
  • JavaScript - Search()
  • JavaScript - Split()
  • JavaScript - Substr()
  • JavaScript - Blink()
  • JavaScript - Anchor()
  • JavaScript - Big()
  • JavaScript - Strings Object
  • JavaScript - String Format
  • JavaScript - Length Property
  • JavaScript - Prototype
  • JavaScript - Slice Substr Vs Substring
  • JavaScript - JavaScript:void(0)

Other Links

  • JavaScript - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW