JavaScript

JavaScript Substring / jQuery Substring Examples

In JavaScript, the substring() method extracts the characters from a string, between two specified indices, and returns the new sub string. So if you want to find out how to use Substring in JavaScript / jQuery, then this article is for you as the solution is the same.  Examples follow. So read on to find out more.

How to use Substring in JavaScript / jQuery

So as the definition says, substring() method extracts the characters from a string by extracting the characters in a string between “from” and “to”. This will exclude  the “to” itself.

Example: Use Substring in jQuery/JavaScript

<html>
<head>
<title>Examples to use JavaScript Substring / jQuery Substring</title>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
</head>

<body>

<script>

var str="This is some custom text";
document.write(str.substring(6)); //O/P: s some custom text
document.write("<br>");

document.write(str.substring(4,1)); //O/P: his
document.write("<br>");

document.write(str.substring(4,4)); //O/P: 
document.write("<br>");

document.write(str.substring(4,10)); //O/P: is so
document.write("<br>");

document.write(str.substring(1,10)); //O/P: his is so
document.write("<br>");

document.write(str.substring(1,20)); //O/P: his is some custom 
document.write("<br>");

</script>

</body>
</html>

 That’s it!

Do you know of any other ways to use substring using jQuery / JavaScript? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*