Java

Substring in Java – String

While managing the data in Java, there can be instances where the developer needs to

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

string.substring(start, last)

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:

String givenString = "Java";
System.out.println("The given string is: "+givenString);
System.out.println("The extracted substring from end is: "+givenString.substring(3));

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:

String givenString = "Java";
System.out.println("The given string is: "+givenString);
System.out.println("The extracted substring at "
+ "specified indexes is: "+givenString.substring(0, 2));

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:

import java.util.Scanner;

Now, add the following code in the “main()” method:

Scanner input = new Scanner(System.in);
System.out.println("Enter the String value: ");
String stringValue = input.nextLine();
System.out.println("The extracted substring is: "+stringValue.substring(0, 2));

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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.