How to use the Java substring method

Java’s substring method is used to extract a substring from a larger string. There are two different ways to use it, either separating the string from an index to the end or with a pre-defined beginning and ending index for the substring.

What is the Java substring method and how is it used?

In Java, characters, digits and special characters can be combined into a Java string. If you want to extract a specific subset from the larger string, you have several options. The method Java String.split() breaks an entire string down into its individual parts to, for example, give you a clearer overview of the string. The method substring() takes that one step further and returns only the part of the string that you define. You can define which part of the string you want to extract using solely a start index or with both a start and end index. In this tutorial, we’ll introduce Java’s substring() method in its different forms.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups

How to use Java substring() with beginIndex

As mentioned above, there are two ways to use Java’s substring() method. The first only defines the starting point of the substring that you want to extract. It breaks up the string from that index to the last character of the string. Its syntax looks as follows:

String substring(int beginIndex)
java

To use substring(), you’ll first enter the string that you want to extract the substring from. Then you use an integer to define where the substring should begin. You can view the output with the Java command System.out.println. The method works inclusively, meaning that the character that is in the position of the index you enter will also be separated. If the value of beginIndex is smaller than 0 or larger than the actual length of the string, you’ll get an error message.

Let’s look at a simple example to see how exactly substring() works in Java. We’ll create a string and then extract a substring.

public class Main {
	public static void main(String args[]) {
	String Str = new String("This is an example. This is another sentence.");
	System.out.print("The substring is: ");
	System.out.println(Str.substring(20));
	}
}
java

When you run the code, you’ll get the following output:

The substring is: This is another sentence.
java

Of course that works just as well with digits:

public class Main {
	public static void main(String args[]) {
	String Str = new String("192.448.782.1142");
	System.out.print("The last four digits are: ");
	System.out.println(Str.substring(12));
	}
}
java

Here is the output:

The last four digits are: 1142
java

How to use substring() with beginIndex and endIndex

The second way to use Java’s substring() method allows for more control over the resulting substring. You’ll not only specify the starting point of the substring but also an end point. The syntax looks as follows:

String substring(int beginIndex, int endIndex)
java

The beginIndex is inclusive, and the endIndex is exclusive. Let’s once again look at a simple example:

public class Main {
	public static void main(String args[]) {
	String Str = new String("This is an example. This is another sentence.");
	System.out.print("The substring is: ");
	System.out.println(Str.substring(8, 18));
	}
}
java

The output is:

The substring is: an example
java

You can also easily extract a substring from within a single word. In the next example, we’ll create a grocery list:

public class Main {
	public static void main(String args[]) {
	String Str = new String("Eggs, Honey, Milk, Bread");
	System.out.print("The substring is: ");
	System.out.println(Str.substring(7, 10));
	}
}
java

The new output will look as follows:

The substring is: one
java

In our last example we’ll show a practical use case for Java’s substring() method. Imagine that you’ve received customer data in a certain format. If you want to isolate just a part of that information, substring() is perfect. Let’s look at the code:

public class Main {
	public static void main(String args[]) {
	String Str = new String("247: Taylor, Javal|taylorj@example.com|New York|12.04.1972");
	System.out.print("Here is the customer’s email address: ");
	System.out.println(Str.substring(19, 38));
	}
}
java

Here is the output:

Here is the customer’s email address: taylorj@example.com
java
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top