Yahoo India Web Search

Search results

  1. The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string. If start is greater than end, arguments are swapped: (4, 1) = (1, 4).

    • Overview
    • Syntax
    • Description
    • Examples
    • Browser compatibility
    • See also

    The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.

    Parameters

    indexStart The index of the first character to include in the returned substring. indexEnd Optional The index of the first character to exclude from the returned substring.

    Return value

    A new string containing the specified part of the given string.

    substring() extracts characters from indexStart up to but not including indexEnd. In particular:

    •If indexEnd is omitted, substring() extracts characters to the end of the string.

    •If indexStart is equal to indexEnd, substring() returns an empty string.

    •If indexStart is greater than indexEnd, then the effect of substring() is as if the two arguments were swapped; see example below.

    Any argument value that is less than 0 or greater than str.length is treated as if it were 0 and str.length, respectively.

    Any argument value that is NaN is treated as if it were 0.

    Using substring()

    The following example uses substring() to display characters from the string "Mozilla":

    Using substring() with length property

    The following example uses the substring() method and length property to extract the last characters of a particular string. This method may be easier to remember, given that you don't need to know the starting and ending indices as you would in the above examples.

    The difference between substring() and substr()

    There are subtle differences between the substring() and substr() methods, so you should be careful not to get them confused. •The two parameters of substr() are start and length, while for substring(), they are start and end. •substr()'s start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0. •Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start.

    BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

    •String.prototype.substr()

    •String.prototype.slice()

  2. Summary: in this tutorial, you’ll learn how to use the JavaScript substring() method to extract a substring from a string. Introduction to the JavaScript substring() method. The JavaScript String.prototype.substring() returns the part of the string between the start and end indexes:

  3. Jul 12, 2024 · The substring () method in JavaScript extracts characters between two indices from a string, returning the substring from the start to the end index (exclusive). It preserves the original string and is useful for extracting specific portions efficiently. Syntax: string.substring(Startindex, Endindex); Parameters:

  4. substring() Parameters. The substring() method takes in: indexStart - The index of the first character to start including in the returned substring. indexEnd (optional) - The index before which to stop extraction. (Exclusive) If omitted, it extracts till the end of the string.

  5. The JavaScript substring method returns part of a string that is specified through two parameters: the start character and end character of that string. What is the Substring Method? string.substring(startIndex, endIndex); Substring in JavaScript is a method that allows us to select a specific part of a string.

  6. People also ask

  7. Mar 22, 2020 · 1. The substring( ) Method. Let’s start with the substring( ) method. This method basically gets a part of the original string and returns it as a new string. The substring method expects two parameters: string.substring(startIndex, endIndex); startIndex: represents the starting point of the substring