The following example uses the substring method to extract a substring starting from the beginning of the string: let str = 'JavaScript Substring' Ĭonsole.log(substring) Code language: JavaScript ( javascript ) 1) Extracting a substring from the beginning of the string example Let’s take some examples of using the JavaScript substring() method. If any parameter is NaN, the substring() treats it as if it were zero (0). If either startIndex or endIndex is less than zero or greater than the string.length, the substring() considers it as zero (0) or string.length respectively. If startIndex is greater than the endIndex, the substring() swaps their roles: the startIndex becomes the endIndex and vice versa. If startIndex equals endIndex, the substring() method returns an empty string. If you omit the endIndex, the substring() returns the substring to the end of the string. In other words, the returned substring doesn’t include the character at the endIndex. The endIndex determines the first character to exclude from the returned substring.The startIndex specifies the index of the first character to include in the returned substring.The substring() method accepts two parameters: startIndexand endIndex: The JavaScript () returns the part of the string between the start and end indexes: str.substring(startIndex ) Introduction to the JavaScript substring() method Summary: in this tutorial, you’ll learn how to use the JavaScript substring() method to extract a substring from a string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |