substring javascript

Substring JavaScript Method

Substring Javascript is an inbuilt function that is used to extraction the given string from start index to end index and returns the new substring.Β 
Note:Β Indexing start from zero (0).

This method extracts the characters in a string between two specified indices, not including the “end” index itself.

If the starting position is greater than the end position, performΒ swapΒ between two arguments, such as str.substring (5, 2) == str.substring (2, 5).

If either start index or end index is less than 0 or negative, it considers that index as 0.

Substring Javascript Examples

<html>
<head>
<title>Developer Helps | JavaScript Substring</title>
</head>
<body>

<p>Click the "Check it" button to extract substring functionality.</p>

<button onclick="customFun()">Check it</button>

<p id="substring-test"></p>

<script>
function customFun() {
  var original_str = "Developer Helps!";
  var resultant_str = original_str.substring(0, 3);
  document.getElementById("substring-test").innerHTML = "Resultant String : " + resultant_str;
}
</script>
</body>
</html>

Output : (Before Click on Check it button)

Click the "Check it" button to extract substring functionality.

Check it

(After Click on Check it button)

Click the "Check it" button to extract substring functionality.

Check it

Resultant String: Dev

String extraction begin at position 2, and extract the rest of the string :

<html>
<head>
<title>Developer Helps | JavaScript Substring</title>
</head>
<body>

<p>Click the "Check it" button to extract substring functionality.</p>

<button onclick="customFun()">Check it</button>

<p id="substring-test"></p>

<script>
function customFun() {
  var original_str = "Developer Helps!";
  var resultant_str = original_str.substring(3);
  document.getElementById("substring-test").innerHTML = "Resultant String : " + resultant_str;
}
</script>
</body>
</html>

Output : (After Click on Check it button)

Click the "Check it" button to extract substring functionality.

Check it

Resultant String: eloper Helps!

If starting position is greater than end position, perform swap between two arguments :

<html>
<head>
<title>Developer Helps | JavaScript Substring</title>
</head>
<body>

<p>Click the "Check it" button to extract substring functionality.</p>

<button onclick="customFun()">Check it</button>

<p id="substring-test"></p>

<script>
function customFun() {
  var original_str = "Developer Helps!";
  var resultant_str = original_str.substring(5,2);
  document.getElementById("substring-test").innerHTML = "Resultant String : " + resultant_str;
}
</script>
</body>
</html>

Output : (After Click on Check it button)

Click the "Check it" button to extract substring functionality.

Check it

Resultant String: vel

If starting position is less than 0 or negative, extraction start from position 0 :

<html>
<head>
<title>Developer Helps | JavaScript Substring</title>
</head>
<body>

<p>Click the "Check it" button to extract substring functionality.</p>

<button onclick="customFun()">Check it</button>

<p id="substring-test"></p>

<script>
function customFun() {
  var original_str = "Developer Helps!";
  var resultant_str = original_str.substring(-2);
  document.getElementById("substring-test").innerHTML = "Resultant String : " + resultant_str;
}
</script>
</body>
</html>

Output : (After Click on Check it button)

Click the "Check it" button to extract substring functionality.

Check it

Resultant String: Developer Helps!

Extract first character from the string :

<html>
<head>
<title>Developer Helps | JavaScript Substring</title>
</head>
<body>

<p>Click the "Check it" button to extract substring functionality.</p>

<button onclick="customFun()">Check it</button>

<p id="substring-test"></p>

<script>
function customFun() {
  var original_str = "Developer Helps";
  var resultant_str = original_str.substring(0,1);
  document.getElementById("substring-test").innerHTML = "Resultant String : " + resultant_str;
}
</script>
</body>
</html>

Output : (After Click on Check it button)

Click the "Check it" button to extract substring functionality.

Check it

Resultant String: D

Extract last character from the string :

<html>
<head>
<title>Developer Helps | JavaScript Substring</title>
</head>
<body>

<p>Click the "Check it" button to extract substring functionality.</p>

<button onclick="customFun()">Check it</button>

<p id="substring-test"></p>

<script>
function customFun() {
  var original_str = "Developer Helps";
  var resultant_str = original_str.substring(original_str.length - 1, original_str.length);
  document.getElementById("substring-test").innerHTML = "Resultant String : " + resultant_str;
}
</script>
</body>
</html>

Output : (After Click on Check it button)

Click the "Check it" button to extract substring functionality.

Check it

Resultant String: s

Thanks for the reading post. I hope you like and understand the post. If you have any doubts regarding this post please comment below.

More Related Post

Leave a comment

Your email address will not be published. Required fields are marked *