![]() ![]() String filename '' // full file name int iend filename.indexOf ('.') //this finds the first occurrence of '.' //in string thus giving you the index of where it is in the string // Now iend can be. This is how you use indexOf and substring functions together. Let's understand the startIndex and endIndex by the code given below. The accepted answer is correct but it doesn't tell you how to use it. The method throws an IndexOutOfBoundException when the startIndex is less than zero or startIndex is greater than endIndex or endIndex is greater than length of String. This method returns new String object containing the substring of the given string from specified startIndex to endIndex. public String substring(int startIndex, int endIndex):.The method throws an IndexOutOfBoundException when the startIndex is larger than the length of String or less than zero. This method returns new String object containing the substring of the given string from specified startIndex (inclusive). public String substring(int startIndex):.You can get substring from the given String object by one of the two methods: Suppose the string is " computer", then the substring will be com, compu, ter, etc. In case of substring() method startIndex is inclusive and endIndex is exclusive. You want to get the st1 string without last two characters, so when we use the substring() in Java we have to give the start point and end point of the sub-string. Java String class provides the built-in substring() method that extract a substring from the given string by using the index values passed as an argument. ![]() In other words, substring is a subset of another String. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |