utilize the contained data or modify it. For instance, using a string value differently or updating the values in accordance with the given requirement keeps the “document design” intact. In such cases, substring in Java assists in utilizing the current resources and easing the functionalities at the developer’s end.
This blog will demonstrate the usage and implementation of the “substring” in Java.
What is a “Substring” in Java?
“String substring” in Java can be achieved via the “substring()” method. This method extracts and returns a substring from the provided or user-input string values based on the specified start and end indexes.
Syntax
In the above syntax:
- “start” refers to the start index.
- “last” corresponds to the end index.
Example 1: Retrieve a Substring of a String From the Last in Java
In this example, the specified string can be substring from the last:
In the above code snippet:
- Firstly, initialize the string value and display it.
- In the next step, associate the “substring()” method with the specified string by pointing to the given index.
- This will result in extracting the last string character, thereby extracting the substring from the last.
Output
This output signifies that the last string character is retrieved corresponding to the index “3”.
Example 2: Retrieve a Substring of a String With Respect to Specified Indexes in Java
In this particular example, a string in Java can be substring at specific indexes:
In the above code block:
- Similarly, initialize the string value and display it.
- Now, apply the “substring()” method such that the string is substring from the start, i.e.,”0” till the end index, i.e., “2”.
- Note that the value against the end index “2” is excluded in the substring extraction process.
Output
From the above output, it can be implied that the string is substring accordingly.
Example 3: Retrieve a Substring of a User-Input String in Java
In this case, the string will be taken as input from the user and substring accordingly. Firstly, include the below-provided library to enable user input:
Now, add the following code in the “main()” method:
In the above code:
- Create a “Scanner” object named “input” using the “new” keyword and the “Scanner()” constructor, respectively.
- The “in” parameter takes the user input.
- In the next step, the associated “nextLine()” method ensures the user input as a “string”.
- Finally, apply the “substring()” method to substring the user input string in accordance with the specified indexes as its(method) parameter.
Output
In this outcome, it can be analyzed that the user-defined string is substring accordingly.
Conclusion
Extracting the substring from a string in Java can be carried out via the “substring()” method. This method extracts and gives a substring from the string based on the specified indexes. Moreover, it can be utilized to retrieve the substring of the string from the last, at specified indexes, or substring a user input string, respectively. This blog discussed the approaches to “substring” a string in Java.