JavaScript substr() Method
JavaScript substr() method This method returns the specified number of characters from the specified index from the given string. It extracts a part of the original string.
Example: This example shows the implementation of the above-explained approach.
// Define a string variable 'str'
let str = "Mind, Power, Soul";
// Use the substr() method to extract a substring f
let part = str.substr(6, 5);
// Output the value of variable
console.log(part);
Output
Power
Explanation:
The code initializes a string str with the value “Mind, Power, Soul”. It then uses the substr() method to extract a substring starting from index 6 and including the next 5 characters. The extracted substring “Power” is stored in the variable part, and then it is printed to the console.
JavaScript String Methods
JavaScript strings are the sequence of characters. They are treated as Primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing.
JavaScript provides a bunch of string methods for representing and manipulating strings. From retrieving specific characters with charAt(), converting strings to uppercase with toUpperCase(), to combining strings with concat(), these methods simplify a wide range of tasks for developers. In this article, we will explore some essential JavaScript string methods that every developer should be familiar with.