The Java substring() method returns a portion of a particular string. This method accepts the start and end index values of the characters you would like to retrieve from a string. The original string on which substring() is applied is left unchanged.
You may want to retrieve a particular part of a string in a Java program.
That’s where the Java substring() method comes in. The substring() method returns a substring from a string starting from a specific index value and ending at an optional index value.
This tutorial will explore how to use the substring() method in Java, and explore a few examples of the string method in a program.
Java Substring Syntax
The string substring() method retrieves a sequence of characters from a string. These characters are retrieved based on their index values.
The substring() method is applied to the end of a string like so:
public String substring(startIndex, endIndex)
substring() accepts two parameters:
- start is the index position from which the substring should begin, and is inclusive (required)
- end is the index position at which the substring should end and is exclusive. By default, the end value is equal to the length of a string. (optional)
Strings in Java are indexed. This means that you can retrieve individual characters from a particular string.
For instance, you can retrieve the first character in a string by referencing its Java index number, which is always 0.
Index numbers are assigned starting from 0 and increase for every new character in a string, including spaces, letters, numbers, and symbols.
An StringIndexOutOfBoundsException is raised if you specify an index value that is greater than the length of the string with which you are working. You must make sure that you retrieve the correct part of a string to avoid this error.
Java Substring() Examples
String Substring in Java with No End Value
Say that we are operating a coffee shop. After an order is placed, a string is created in a computer program. This string stores the order number, name of the coffee, and the customer’s name.
We want to create a program that retrieves every character in our string after the order number. The order number is three characters long. This information is given to the barista, who will brew the coffee order.
To retrieve the characters after the order number, we can use the substring() method. Because we are retrieving all characters after a specific one, we do not need to specify an end parameter:
public class GetOrderData { public static void main(String[] args) { String order = new String("020 Latte Eileen"); System.out.println(order.substring(3)); } }
Our code returns: Latte Eileen. We can see our string extends to the character at the end of the string.
We define a public class called GetOrderData. This class retrieves the coffee a customer has ordered and their name. We define a variable that stores the order. This lets us we refer to the customer’s order throughout our code.
Then, we use the substring() method to retrieve every character after the index value 3. This is inclusive of index position 3.
We finally use the printIn() method to print out the customer’s name and coffee order to the console.
String Substring in Java with an End Value
Let’s say that we only want to retrieve the order number for the coffee, which is three numbers long. This is displayed on a screen to indicate the customer’s order is about to be processed.
The order number exists at the start of the order string. The order number is three characters long. We can use substring() to retrieve the order number:
public class GetOrderNumber { public static void main(String[] args) { String order = new String("020 Latte Eileen"); System.out.println(order.substring(0, 3)); } }
Our code returns the following: 020
.
We use the substring() method to retrieve the characters between the index positions 0 and 3 in our string. These characters contain the order number that we want to retrieve.
Remember, string indexes start at 0. The end parameter is exclusive of the final value you specify (in this case, 3). This is why we have specified the index values 0 to 3.
Conclusion
The substring() method extracts characters from an existing string. substring() accepts start and end index values at which the substring should start and end, respectively. This method creates a new string. It does not modify an existing string.
You’re now equipped with the knowledge you need to use the Java substring() method with confidence! Read our guide on how to learn Java for information on what you may want to learn next.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.