javascript
  1. javascript-substr

JavaScript substr()

The Javascript substr() method is used for extracting a portion of a string, starting from the specified index position, and returns a new sub-string. It takes two arguments:

  • The starting index position to begin extraction. The index is zero-based.
  • The length of the sub-string to be extracted. If this parameter is not specified, the method extracts all the characters from the starting index position till the end of the string.

Syntax

string.substr(start, length)
  • string: The original string.
  • start: The position in the string to start extraction (required).
  • length: The number of characters to be extracted (optional).

Example

let str = "JavaScript substr() method";

console.log(str.substr(0, 10)); // "JavaScript"
console.log(str.substr(11));    // "substr() method"
console.log(str.substr(1));     // "avaScript substr() method"

Output

"JavaScript"
"substr() method"
"avaScript substr() method"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>substr() Method Example</title>
</head>
<body>

<!-- Display area for the output -->
<div id="output"></div>

<script>
    // Your JavaScript code
    let str = "JavaScript substr() method";

    // Using substr() method
    const result1 = str.substr(0, 10);
    const result2 = str.substr(11);
    const result3 = str.substr(1);

    // Display the output in the HTML document
    document.getElementById('output').innerHTML = `
        <p>str.substr(0, 10): "${result1}"</p>
        <p>str.substr(11): "${result2}"</p>
        <p>str.substr(1): "${result3}"</p>
    `;
</script>

</body>
</html>
Try Playground

Explanation

In the above example, substr() method is used to extract sub-strings from the str string, starting from various positions with different lengths.

  • console.log(str.substr(0, 10)) returns a sub-string starting from the first character of the string (J) and has a length of 10 characters, which results in "JavaScript".
  • console.log(str.substr(11)) returns a sub-string starting from the twelfth character of the string (s) till the end of the string, which results in "substr() method".
  • console.log(str.substr(1)) returns a sub-string starting from the second character of the string (a) till the end of the string, which results in "avaScript substr() method".

Use

The JavaScript substr() method can be used in various applications for extracting excerpts of a string based on pattern matching or to extract specific portions of a string.

Important Points

  • The substr() method is a zero-based index, meaning the first character has an index of 0, second has an index of 1, and so on.
  • If the length argument is not specified, it extracts all the characters from the start position till the end of the string.
  • If the start argument is negative, it begins extraction from the end of the string.

Summary

The substr() method is a powerful Javascript string method used for extracting portions of a string based on the specified index positions and length. With its simple syntax and wide range of applications, it enables developers to manipulate and extract meaningful data from strings.

Published on: