![]() ![]() Here, the parameter beginIndex is inclusive. Substring(int beginIndex) returns the substring of this string which will start from the character at the beginIndex and will extend to the end of the string. In Java substring() method has two variants. I.e substring(int beginIndex, int endIndex) will throw an exception ifīeginIndex is negative OR beginIndex > endIndex OR endIndex > string.length() Conclusion Substring(int beginIndex, int endIndex) method will throw IndexOutOfBoundsException exception if beginIndex is negative, or beginIndex larger than the endIndex or endIndex is larger than the length of the string object. ![]() beginIndex The substring will start from the specified beginIndex and it will extend to the end of the string. The first variant of the substring() method accepts only one parameter. We will learn about both variants one by one. substring(int beginIndex, int endIndex).In Java, the substring() method is the overloaded method and it has two variants. The creation of the ‘substring’ depends on the parameter passed to the substring() method. Java substring() method returns a ‘substring’ of the specified string. For instance, “the best of” is a substring of “It was the best of times”. “A substring is a contiguous sequence of characters within a string. Let us understand what does mean by ‘substring’. I have given two examples with the pictorial explanation to make this concept easy to understand. After reading this article you will be able to use the ‘substring()` method of the String class. The String class in Java is one of the most important classes in Java. In this article, we have covered what is a Substring in Java. ![]() In other words, substring() when used with the endIndex parameter returns a string of length endIndex-startIndex. Here startIndex is 11 and endIndex is 15, which means it returns a 4 letter word starting from the character “J”. String strnewValue = strValue.subString(11,15) The below example shows how to fetch the string”Java””. In case we need to fetch a string of specified length, then we pass both startIndex and endIndex as parameters. Using startIndex and endIndex as parameters String strnewValue = strValue.subString(11) String strValue = "Welcome to Java tutorial" ![]() Since we are not specifying any endIndex, the entire string from index 11 will be returned. In this case, we pass the startIndex parameter as 11. Exampleįrom the given input string, we need to retrieve the string “Java tutorial””. If we want to retrieve a substring from a particular index until the end of the string, then we pass only startIndex as a parameter. We can use the substring() method in 2 different ways as mentioned below: Ways of using Java Substring Method 1. Substring in Java throws IndexOutOfBoundException in below cases: Return value – New substring Java Substring Exceptions endIndex always starts with 1 from the start of the string. startIndex always starts with 0 from the start of the string.ĮndIndex – The index until which we need to retrieve the values. StartIndex – The index from which we need to fetch the part of the string. Public String substring(int startIndex, int endIndex) Using startIndex and endIndex as parameters Java Substring Syntax ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |