The JavaScript substring() returns the substring from a string between the start and end indexes.Then the substring returns the domain that starts from the index of plus 1 to the end of the string.First, the indexOf() returns the position of the character.The following example uses the substring() with the indexOf() to extract the domain from the email: let email = domain = email.substring(email.indexOf( + 1) Ĭonsole.log(domain) // Code language: JavaScript ( javascript ) The following example uses the substring() to extract a substring from the index 11 to the end of the string: let str = 'JavaScript Substring' Ĭode language: JavaScript ( javascript ) 3) Extracting domain from the email example Output: JavaScript Code language: JavaScript ( javascript ) 2) Extracting a substring to the end of the string example 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 ![]() Let's understand the startIndex and endIndex by the code given below.Summary: in this tutorial, you’ll learn how to use the JavaScript substring() method to extract a substring from a string. Public String substring(int startIndex, int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex. Public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive). ![]() In case of substring() method startIndex is inclusive and endIndex is exclusive. You can get substring from the given string object by one of the two methods: In other words, substring is a subset of another String. It throws IndexOutOfBoundsException If the beginIndex is less than zero OR beginIndex > endIndex OR endIndex is greater than the length of String. "Chaitanya".substring(2,5) would return "ait". String substring(int beginIndex, int endIndex): Returns the substring starting from the given index(beginIndex) till the specified index(endIndex). ![]() This method throws IndexOutOfBoundsException If the beginIndex is less than zero or greater than the length of String (beginIndex length of String). "Chaitanya".substring(2) would return "aitanya". ![]() String substring(int beginIndex): Returns the substring starting from the specified index(beginIndex) till the end of the string.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |