# 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](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-strings/). If you want to extract a specific subset from the larger string, you have several options. The method [Java String.split()](https://www.ionos.com/en-ie/digitalguide/websites/web-development/string-splitting-in-java/) 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.

## 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:

```java
String substring(int beginIndex)
```

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.

```java
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));
	}
}
```

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

```java
The substring is: This is another sentence.
```

Of course that works just as well with digits:

```java
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));
	}
}
```

Here is the output:

```java
The last four digits are: 1142
```

## 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:

```java
String substring(int beginIndex, int endIndex)
```

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

```java
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));
	}
}
```

The output is:

```java
The substring is: an example
```

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

```java
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));
	}
}
```

The new output will look as follows:

```java
The substring is: one
```

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:

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

Here is the output:

```java
Here is the customer’s email address: taylorj@example.com
```


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-substrings/](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-substrings/) for AI/LLM consumption.