![]() ![]() If the endingIndex is 2, the substring will be wrongly extracted as “we”. The startingIndex is always inclusive and the endingIndex is always exclusive.įor example: From the String “welcome”, if you want to extract the substring “wel”, the startingIndex will be 0, and the endingIndex will be 3. The method throws a StringIndexOutOfBoundException if,Ī) the startingIndex is greater than the endingIndex, or negative, or greater than the length of the given string, or,ī) endingIndex is greater than the length of the given string or negative. It returns a new String object which contains the required substring from the given startingIndex to endingIndex. To understand how the substring () method works, let’s look at an example. Below, we’ll look into the use in more detail. This function does not alter the given string. string.substring (int startIndex, int endIndex) The startIndex is inclusive while the endIndex is exclusive in this instance, and the string is an object of the String class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |