How To Use String Object Substr In JavaScript

In this article I am going to explain about String Object Substr In JavaScript.
  • 1923

Use substr() method in JavaScript

The substr() method is used to read the all character set and select the position of specified character set, and return a specified number of character.

This method extract characters from the end of the string.

This method use a negative start number.

The substr() method does not change the original string in JavaScript.

All browsers support string object substr method, (This does not work in IE8).

Syntax

string.substr(start,length)

Example

<!DOCTYPE html>

<html>

<body style ="background-color :Red">

 

<p id="demo">Extract parts of the string.</p>

 

<button onclick="myFunction()">Click here</button>

 

<script type="text/javascript">

    function myFunction() {

        var str = "Welcome India!";

        var n = str.substr(3, 5);

        document.getElementById("demo").innerHTML = n;

    }

</script>

 

</body>

</html>

Output1

 op1.jpg

Output2

 op2.jpg

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.