JavaScript substring Method

Introduction to JavaScript substring

JavaScript substring is a built-in JavaScript function that returns the portion of the provided string from a start index to an end index. The indexing begins at zero (0).

Syntax:

string.substring(Startindex, Endindex)

Parameters:

  • Here the Startindex and Endindex describe the part of the string to be taken as a substring. Here the Endindex is optional.

Return value:

  • It returns a new string that is part of the given string.

Code Examples of JavaScript substring

Code Example 01: JavaScript code to show the working of string.substring() function

var string = "WelcomeToSofthunt.netTutorialWebsite";
one = string.substring(0, 7)
two = string.substring(7, 9)
three = string.substring(9,21)
four = string.substring(21,29)
five = string.substring(29,36)
six = string.substring(0)
 
document.write(one);
document.write(two);
document.write(three);
document.write(four);
document.write(five);
document.write(six);

Output:

Welcome
To
Softhunt.net
Tutorial
Website
WelcomeToSofthunt.netTutorialWebsite

Code Example 02: The index always begins with 0. If we continue to accept the index as negative, it will be treated as zero, and the index cannot be in a fraction; if it is, it will be transformed to its nearest whole number.

var string = "WelcomeToSofthunt.netTutorialWebsite";
one = string.substring(-1, 7)
two = string.substring(7.3, 9)
three = string.substring(9,21)
four = string.substring(21,29)
five = string.substring(29,36)
six = string.substring(-34)
 
document.write(one);
document.write(two);
document.write(three);
document.write(four);
document.write(five);
document.write(six);

Output:

Welcome
To
Softhunt.net
Tutorial
Website
WelcomeToSofthunt.netTutorialWebsite

Substring() vs Substr() vs Slice()

These methods are used to extract parts of a string and return the extracted parts in a new string. All of these do not change the original string.

JavaScript substring()

JavaScript substring() method returns part of the string between startIndex & endIndex. It excludes the endIndex character or up to the end of the string.

Syntax:

string.substring(Startindex, Endindex)

JavaScript substr()

JavaScript substr() method returns part of the string starting from an index and the number of characters after the start index. It has two parameters such as startIndex and Length

Syntax:

string.substr(startIndex[, length])

Code Example:

var string = "WelcomeToSofthunt.netTutorialWebsite";
one = string.substr(0, 7)
two = string.substr(7, 2)
three = string.substr(9,12)
four = string.substr(21, 8)
five = string.substr(29,36)
six = string.substr(0)
 
document.write(one);
document.write(two);
document.write(three);
document.write(four);
document.write(five);
document.write(six);

Output:

Welcome
To
Softhunt.net
Tutorial
Website
WelcomeToSofthunt.netTutorialWebsite

JavaScript slice()

slice() method is similar to substring. It returns a partial string between the start index & end index. It excludes the end index character or up to the end of the string if the end index is not provided.

Syntax:

string.slice(startIndex[, endIndex])

Code Example:

var string = "WelcomeToSofthunt.netTutorialWebsite";
one = string.slice(0, 7)
two = string.slice(7, 9)
three = string.slice(9,21)
four = string.slice(21,29)
five = string.slice(29,36)
six = string.slice(0)

 
document.write(one);
document.write(two);
document.write(three);
document.write(four);
document.write(five);
document.write(six);

Output:

Welcome
To
Softhunt.net
Tutorial
Website
WelcomeToSofthunt.netTutorialWebsite

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

Leave a Comment