“The java substring is a part of the java main string. As the java strings are immutable, the initial string is left unchanged, and a new string is returned by this function. The new string is the substring from the string. To extract the desired substring using the substring method, we must provide the starting index and ending index as input. The substring returned from this method depends on the index value assigned to it.”
Given below is a simple demonstration of the java substring method. The substring method extracts the substring from the given string. The substring method takes the index value that starts from that character index till the end index of the character in the specified string.
We have created the java class “example1,” which is publicly declared. Inside the class “example1”, we have the main method implementation. Here, we have defined the string variable “My_Str,” which contains the string of words. Then, the java system.out.print method is employed with the substring method. The substring method takes the index value “15”. The substring extract from the “My_Str” starts from the index character “15”.
The console screen displays the substring that is obtained from the specified string in the above program.
Now, we are using another way to demonstrate the substring method. Additionally, the start index value and the end index value are two inputs that the substring method accepts. The start index starts from the zero value, and the end index begins with the value -1. The substring is obtained from the string lying in between the start and end index.
We have defined the java class “example2,” where the main method of the program is performed. Here, we have initialized the string inside the variable “Str_Value”. We have two strings, “example2” and “java,” within the string. We have to obtain the substring from these two strings. For this, we have defined the “Begin” variable where the indexOf method is utilized, which searches the first index string.
We have also fetched the length of the string because the character at the end of the string, “example2,” is required. Likewise, we have searched the initial index of the string “java” inside the variable “End”. Then, we invoked the substring method and passed the “Begin” and “End” variables as input to acquire the substring between both indices. The “Str_Result” will print the substring to the console.
We have got the substring as follows from two strings by specifying the start and end indices.
In this example, we are taking some important case scenarios of the substring method of java. We have some key points regarding the java substring method which should be followed. The substring returns an empty string when we have an equal starting and ending index. The substring also returns empty when the start index of the substring matches the length of the substring. Moreover, when the start index is lesser than a zero value or the start index is greater than the end index, the substring method returns “IndexOutOfBoundException”.
All the above key points here are demonstrated programmatically. In the program, we have class “example3,” where the main method is called. We have declared the variable here as “string_word” and initialized this variable with the string. Firstly, we have obtained the substring, which starts from the index value “27”. The index value assigned in the substring method and the substring method is called within the variable “s1”.
Then, we have another variable, “s3,” where we have called the substring method again, but this time the length of the substring is required. The length of the substring is passed as the start index, so it will return the empty string, which we have checked via the “isEmpty” method. The next scenario involves determining whether the start and end indexes are the same. For this, we have defined the variable “s3” and passed the same value, “5,” for starting and ending the index. Lastly, we have a case of “IndexOutOfBoundException”. We declared the variables “s4” and “s5” and defined them with the substring method. The substring method of “s4” has the beginning index value “-1,” which is lesser than the value zero.
On the other hand, we have provided the “s5” substring method with the start index “2” and end index “1”. As the “2” is greater than “1”, the substring will return an “IndexOutOfBoundException” to the console.
The results of the above case scenarios of substring are displayed on the console as follows.
The substring method can also be used to determine whether the specified string is a palindrome or if it can be viewed in an equal direction from both ends.
The java class “example5” is constructed in this particular program. Then, we have the definition of the program’s main method. At the beginning of the main method, we have initialization of multiple strings, which are defined in “verify_palindrome” simultaneously. Next, we have constructed the “Verify_Palindrome” function with the boolean data type. The results from the “Verify_Palindrome” will be printed as true or false.
Here, the function “Verify_Palindrome” takes the string object as “Str”. Within the function, we have used the if statement, which checks whether the string is null or not. We have checked the string length should be greater or equal to a value of “1,” which is also considered a palindrome within the second “if” statement. Then, we have the usage of the substring method, where we obtain the initial and last character of the string.
The last “if” statement verifies the palindrome with the condition that the start and end index is equal or not. If they are not the same, we removed the first and the last letter from the substring and then repeated the process to verify the palindrome. The “return” statement will provide the next recursion results.
The following are the Boolean values that determine which of the provided string is a palindrome or not.
All in all, with this article, we have learned about the substring method of java programming. The substring method is very beneficial that is provided by java.lang.String class, which is used to get the smaller strings from the bigger ones. We have seen the scenarios of the substring method of java programmatically. The aforementioned examples illustrate how the internal workings of the substring function change over time.