javascript substring

The javascript substring is used to extract characters from a string. After extracting the characters from the string, the substring function creates a new substring. Let’s know how to use substring in javascript.

<script>
	var mystr1 = “How to use substring in javascript”;

	document.write(mystr1.substring(1,3));            
</script>

Output:- ow


<script>
	var mystr2 = “How to use substring in javascript”;

	document.write(mystr2.substring(2,8));
</script>

Output:- w to u


<script>
	var mystr3 = “How to use substring in javascript”;

	document.write(mystr3.substring(8,15));
</script>

Output:- se subs


<script>
	var mystr4 = “This Function is used to extract characters”;

	document.write(mystr4.substring(10));
</script>

Output:- ion is used to extract characters

Javascript Substring Methods

First Characters:- If you want to get the first characters of the string, so for that you have to see the example given below. In the following example, we have extracted the first 10 characters.

<script>
	var mystr1 = “This Function is used to extract characters”;
	document.write(mystr1.substring(10));
</script>

Middle Characters:- But if you want to extract characters from the middle of a string, then for that you have to follow the example given below. We have extracted 7 to 20 characters from inside our string.

<script>
	var mystr2 = “This Function is used to extract characters”;
	document.write(mystr2.substring(7,20));
</script>

Behind Characters:- If you have a large string whose character you want to extract from behind. So for that, you will first have to count the length of your string. In the following example, we counted the length of our string and then extracted 10 characters from behind.

<script>
	var mystr3 = "How to use substring in javascript";
	var data = mystr3.substring(mystr3.length - 10, mystr3.length);
	document.write(data.substring());
</script>

Other Javascript Substring Examples

Button Onclick

<button onclick=”Stringfunction()”>Click To Extract</button>
<p id=”stringid”> </p>


<script>

	function Stringfunction() {

		var mystring = “This Function is used to extract characters”;

		var data = mystring.substring(10);

		document.getElementById(“stringid”).innerHTML = data;
    }
</script>


Console Log


<script>
	var mystr3 = “This Function is used to extract characters”;
	console.log(mystr3.substring(5,17));
</script>


Alert


<script>
	var mystr3 = “This Function is used to extract characters”;
	alert(mystr3.substring(2, 9));
</script>

Browser Support

  • Edge
  • Firefox
  • Internet Explorer
  • Opera
  • Safari
  • Chrome

Frequently Asked Questions

We have answers to some questions that people search on the Internet everyday. Let us know what are those questions and what are their answers.

How do I link CSS to HTML?
To link CSS with html, you can place your file inside the head tag. For example link rel = “stylesheet” href = “mystyle.css”

How do you put a full background image on HTML?
If you want to apply the full background image, then you can apply the image above your body tag. for example Background-image: URL (“myimage.jpg”).

What is div in HTML?
In simple language, div is a box inside which you set your elements.

How do you underline in HTML?
With the tag, you can underline your text.

Where do I put CSS in HTML?
There are 3 ways to apply CSS, either you can link your file inside the tag, or you can add inline CSS, or you can place your CSS on the same page with the

Note: All the examples you have seen inside this article have all been made by us and tested thoroughly. So you can use all these without any difficulty.

Related: 12 easiest examples of javascript onclick