JS: String.prototype.substring()

The JavaScript substring() function extract a part of a string. It is very useful in many string related programs.

Advertisements

The syntax for .substring() function is given below.

String.substring(startIndex[, endIndex]);

startIndex is number that represents the position of a character from where you want to begin extraction.

Advertisements

endIndex is the number that represents the position of the last character you want to extract. The position always starts at 0.

Note: that the endIndex is optional. If you do not specify endIndex, the substring() function will extract start from a position and extract rest of the string.

Example Program: Substring()

<!DOCTYPE html>
<html>
<head>
   <title>JS: substring() Function</title>
   <meta charset="utf-8">
  <style>
  main{
     width:60%;
	 padding:2%;
	 margin:auto;
	 background:rgba(234,44,144,0.4);
	 text-align:center;
  }
  </style>
</head>
<body>
<main>
<h1>Extract a Substring</h1>
<h2 id="out">Output Here</h2><br/>
<button onclick="extractString()">Extract</button>
</main>
<script>
function extractString() {

var Str = "Tiger is our national animal";

var output = Str.substring(1,10);

document.getElementById("out").innerHTML = output;
  
}
</script>
</body>
</html>

Output: Substring()

Output - Substring() function
Output – Substring() function

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.