Table of Contents
1. Introduction
1.1 Brief overview of strings in Java
In Java, strings are objects that represent a sequence of characters. They are an instance of the String class, which is a part of the java.lang package. The String class provides various methods and utilities to perform operations such as concatenation, comparison, searching, and manipulation of strings.
One of the most important features of the String class in Java is its immutability, meaning that once a String object is created, its content cannot be changed. Any operation that appears to modify a string actually creates a new String object, leaving the original string unchanged.
1.2 Importance of substrings
Substrings are smaller parts of a larger string, which are often used in various programming tasks. They can help in extracting specific pieces of information from a given string, or in parsing and processing text data. Some common use cases for substrings include:
- Extracting names, addresses, or other specific data from a larger text.
- Parsing user input or file content.
- Implementing search algorithms that match patterns within a string.
- Breaking a string into tokens for further processing, such as in natural language processing or data analysis.
In Java, there are multiple ways to obtain substrings from a given string. Each method has its own advantages and is suitable for different situations. In the following sections, we will explore some of the most commonly used methods for obtaining substrings in Java.
2. String.substring() Method
2.1 Explanation of String.substring() method
The substring() method is one of the most commonly used methods to obtain substrings in Java. It is a member of the String class and returns a new string that is a substring of the original string. This method does not modify the original string, as strings are immutable in Java.
2.2 Syntax and parameters
The substring() method has two overloaded versions:
- public String substring(int beginIndex): This version takes one parameter, beginIndex, which is the starting index (inclusive) of the substring. The method returns a substring that starts from the specified index and extends to the end of the original string.
- public String substring(int beginIndex, int endIndex): This version takes two parameters, beginIndex and endIndex. The method returns a substring that starts from the beginIndex (inclusive) and ends at the endIndex (exclusive). If beginIndex is equal to endIndex, the method returns an empty string.
Both versions of the method throw an IndexOutOfBoundsException if the provided indices are not within the bounds of the original string, or if beginIndex is greater than endIndex.
2.3 Examples and outputs
Example 1: Using the single-parameter version of substring()
public class SubstringExample { public static void main(String[] args) { String original = "Hello, World!"; String sub = original.substring(7); System.out.println("Original: " + original); System.out.println("Substring: " + sub); } }
Output:
Original: Hello, World!
Substring: World!
Example 2: Using the two-parameter version of substring()
public class SubstringExample { public static void main(String[] args) { String original = "Hello, World!"; String sub = original.substring(0, 5); System.out.println("Original: " + original); System.out.println("Substring: " + sub); } }
Output:
Original: Hello, World!
Substring: Hello
Example 3: Using substring() with invalid indices
public class SubstringExample { public static void main(String[] args) { String original = "Hello, World!"; try { String sub = original.substring(-1, 5); System.out.println("Substring: " + sub); } catch (IndexOutOfBoundsException e) { System.out.println("Error: " + e.getMessage()); } } }
Output:
Error: begin -1, end 5, length 13
Here is the screenshot of a working example of substring() in the Java Eclipse IDE, demonstrating the extraction of a substring from a given string.

3. String.subSequence() Method
3.1 Explanation of String.subSequence() method
The subSequence() method is another way to obtain substrings in Java. It is a member of the CharSequence interface, which the String class implements. The primary difference between substring() and subSequence() is that the latter returns a CharSequence object rather than a String. However, this CharSequence can easily be converted to a String using the toString() method.
3.2 Syntax and parameters
The subSequence() method has the following signature:
public CharSequence subSequence(int beginIndex, int endIndex)
It takes two parameters, beginIndex and endIndex. The method returns a CharSequence representing a subsequence that starts from the beginIndex (inclusive) and ends at the endIndex (exclusive). If beginIndex is equal to endIndex, the method returns an empty CharSequence.
The method throws an IndexOutOfBoundsException if the provided indices are not within the bounds of the original string, or if beginIndex is greater than endIndex.
3.3 Examples and outputs
Example 1: Using the subSequence() method
public class SubSequenceExample { public static void main(String[] args) { String original = "Hello, World!"; CharSequence subSequence = original.subSequence(0, 5); String sub = subSequence.toString(); System.out.println("Original: " + original); System.out.println("Subsequence: " + sub); } }
Output:
Original: Hello, World!
Subsequence: Hello
Example 2: Using subSequence() with an empty subsequence
public class SubSequenceExample { public static void main(String[] args) { String original = "Hello, World!"; CharSequence subSequence = original.subSequence(5, 5); String sub = subSequence.toString(); System.out.println("Original: " + original); System.out.println("Subsequence: " + sub); } }
Output:
Original: Hello, World!
Subsequence:
Example 3: Using subSequence() with invalid indices
public class SubSequenceExample { public static void main(String[] args) { String original = "Hello, World!"; try { CharSequence subSequence = original.subSequence(-1, 5); System.out.println("Subsequence: " + subSequence); } catch (IndexOutOfBoundsException e) { System.out.println("Error: " + e.getMessage()); } } }
Output:
Error: begin -1, end 5, length 13
4. String.split() Method
4.1 Explanation of String.split() method
The String.split() method is a useful tool for obtaining substrings in scenarios where the substrings are separated by specific delimiters. The method splits the original string around matches of the given regular expression and returns an array of strings containing the substrings.
4.2 Syntax and parameters
The split() method has two overloaded versions:
- public String[] split(String regex): This version takes a single parameter, regex, which is a regular expression used as a delimiter to split the original string. The method returns an array of strings containing the substrings.
- public String[] split(String regex, int limit): This version takes two parameters, regex and limit. The regex parameter is the regular expression used as a delimiter, and the limit parameter controls the number of substrings in the resulting array.
- If the limit is positive, the method will split the string into at most limit substrings.
- If the limit is negative, the method will split the string as many times as possible.
- If the limit is zero, the method will split the string as many times as possible, but discard any trailing empty strings.
4.3 Extracting substrings using delimiters
The split() method is particularly useful when the substrings you want to extract are separated by a specific delimiter. By providing the delimiter as a regular expression, you can easily split the original string and obtain the desired substrings.
4.4 Examples and outputs
Example 1: Using the single-parameter version of split()
public class SplitExample { public static void main(String[] args) { String original = "apple,banana,orange"; String[] substrings = original.split(","); System.out.println("Original: " + original); System.out.println("Substrings:"); for (String sub : substrings) { System.out.println(" - " + sub); } } }
Output:
Original: apple,banana,orange
Substrings:
- apple
- banana
- orange
Example 2: Using the two-parameter version of split() with a limit
public class SplitExample { public static void main(String[] args) { String original = "apple,banana,orange"; String[] substrings = original.split(",", 2); System.out.println("Original: " + original); System.out.println("Substrings (limit = 2):"); for (String sub : substrings) { System.out.println(" - " + sub); } } }
Output:
Original: apple,banana,orange
Substrings (limit = 2):
- apple
- banana,orange
Example 3: Splitting a string using a more complex regular expression
public class SplitExample { public static void main(String[] args) { String original = "apple--banana-.-orange"; String[] substrings = original.split("--|-\\.-|-"); System.out.println("Original: " + original); System.out.println("Substrings:"); for (String sub : substrings) { System.out.println(" - " + sub); } } }
Output:
Original: apple--banana-.-orange
Substrings:
- apple
- banana
- orange
5. Using Regular Expressions
5.1 Introduction to regular expressions in Java
Regular expressions are a powerful tool for text manipulation and pattern matching in strings. In Java, the java.util.regex package provides classes and methods for working with regular expressions. The key classes in this package are Pattern and Matcher.
5.2 Pattern and Matcher classes
- Pattern: The Pattern class is a compiled representation of a regular expression. It is created using the Pattern.compile() method, which takes a regular expression string as its parameter. Once compiled, a Pattern object can be used to create a Matcher object for a given input string.
- Matcher: The Matcher class interprets the Pattern for a given input string and provides methods for finding matches, extracting substrings, and performing other operations based on the pattern.
5.3 Extracting substrings using regular expressions
To extract substrings using regular expressions, you need to follow these steps:
- Compile a Pattern object using the Pattern.compile() method, providing the regular expression as a parameter.
- Create a Matcher object for your input string by calling the Pattern.matcher() method.
- Use the Matcher.find() method to find matches in the input string, and the Matcher.group() method to extract the desired substrings.
5.4 Examples and outputs
Example 1: Extracting words from a string using a regular expression
import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexExample { public static void main(String[] args) { String original = "Hello, World! 123 Java!"; String regex = "\\b[A-Za-z]+\\b"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(original); System.out.println("Original: " + original); System.out.println("Words:"); while (matcher.find()) { System.out.println(" - " + matcher.group()); } } }
Output:
Original: Hello, World! 123 Java!
Words:
- Hello
- World
- Java
Example 2: Extracting email addresses from a string using a regular expression
import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexExample { public static void main(String[] args) { String original = "Contact us at info@example.com or support@example.org"; String regex = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(original); System.out.println("Original: " + original); System.out.println("Email addresses:"); while (matcher.find()) { System.out.println(" - " + matcher.group()); } } }
Output:
Original: Contact us at info@example.com or support@example.org
Email addresses:
- info@example.com
- support@example.org
6. Apache Commons StringUtils Library
6.1 Overview of Apache Commons StringUtils
Apache Commons StringUtils is a utility library that provides additional functionality for working with strings in Java. It offers several methods for common string operations, including substring extraction, making it a valuable addition to any Java project.
6.2 How to add StringUtils to your project
To use StringUtils in your project, you need to add the Apache Commons Lang dependency to your build file.
For Maven, add the following to your pom.xml:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
For Gradle, add the following to your build.gradle:
implementation 'org.apache.commons:commons-lang3:3.12.0'
6.3 Using StringUtils.substring() method
The StringUtils.substring() method provides an alternative way to obtain substrings in Java. It has a similar syntax to the String.substring() method but offers additional null-safety features.
The method has the following signature:
public static String substring(String str, int start, int end)
It takes three parameters:
- str is the original string,
- start is the beginning index (inclusive),
- and end is the ending index (exclusive).
If the input string is null, the method returns null.
6.4 Examples and outputs
Example 1: Using StringUtils.substring() method
import org.apache.commons.lang3.StringUtils; public class StringUtilsExample { public static void main(String[] args) { String original = "Hello, World!"; String sub = StringUtils.substring(original, 0, 5); System.out.println("Original: " + original); System.out.println("Substring: " + sub); } }
Output:
Original: Hello, World!
Substring: Hello
Example 2: Using StringUtils.substring() with a null input string
import org.apache.commons.lang3.StringUtils; public class StringUtilsExample { public static void main(String[] args) { String original = null; String sub = StringUtils.substring(original, 0, 5); System.out.println("Original: " + original); System.out.println("Substring: " + sub); } }
Output:
Original: null
Substring: null
7. Tips for Efficient Substring Extraction
7.1 Avoiding common pitfalls
When working with substrings in Java, there are some common pitfalls that you should be aware of to ensure your code works correctly and efficiently:
- Index Out of Bounds: Be careful when specifying the start and end indices for substring extraction. If the indices are out of bounds for the original string, a StringIndexOutOfBoundsException will be thrown. Always validate the indices before using them.
- Regular Expression Complexity: When using regular expressions, try to keep them as simple as possible. Complex regular expressions can lead to slow performance and may be difficult to understand and maintain.
- Immutable Strings: Remember that Java strings are immutable. When you extract a substring, a new string object is created, and the original string remains unchanged. This is an important consideration when working with large strings or performing many substring operations.
7.2 Memory considerations
Substring extraction can have implications for memory usage, particularly when dealing with large strings. Here are some tips to help manage memory:
- Use String.intern() method: When extracting many substrings that may have duplicates, consider using the String.intern() method to reduce memory usage. This method returns a canonical representation of the string, which helps to save memory by reusing instances of equal strings.
- Use StringBuilder or StringBuffer: When concatenating multiple strings or substrings, prefer using a StringBuilder or StringBuffer instead of the + operator. These classes are designed for efficient string manipulation and can help reduce memory usage.
7.3 Performance tips
To optimize the performance of your substring extraction code, consider the following tips:
- Use String.split() and String.substring() methods: For simple substring extraction tasks, prefer using built-in Java methods like String.split() and String.substring(). These methods are generally faster than regular expressions for basic string manipulation tasks.
- Precompile Regular Expressions: If you use regular expressions frequently, consider precompiling them using the Pattern.compile() method. This can help improve performance by reducing the overhead of compiling the regular expression each time it is used.
- Profile and Optimize: Always profile your code to identify performance bottlenecks and optimize accordingly. Tools like VisualVM and YourKit can help you analyze the performance of your Java applications and find areas for improvement.
8. Conclusion
8.1 Recap of methods to obtain substrings in Java
In this article, we explored several methods for obtaining substrings from strings in Java:
- String.substring(): A built-in method for extracting substrings based on start and end indices.
- String.subSequence(): Another built-in method that works similarly to String.substring() but returns a CharSequence instead of a String.
- String.split(): A method for splitting a string based on delimiters and extracting substrings.
- Regular expressions: A powerful technique for pattern matching and extracting substrings using the Pattern and Matcher classes.
- Apache Commons StringUtils Library: A utility library that provides additional functionality for working with strings, including substring extraction.
The table below provides an overview of the methods covered in this article and their use cases.
Method | Description | Use Case |
---|---|---|
String.substring() | Extracts a substring based on start and end indices. | Simple substring extraction tasks. |
String.subSequence() | Similar to substring() but returns a CharSequence. | When a CharSequence is required. |
String.split() | Splits a string based on delimiters and extracts substrings. | Extracting substrings using known delimiters. |
Regular expressions | Powerful pattern matching and substring extraction. | Complex patterns and flexible extraction tasks. |
Apache Commons StringUtils | Additional utility library for working with strings. | When null-safety or additional functionality is needed. |
8.2 Choosing the appropriate method based on requirements
Each method we've discussed has its own strengths and weaknesses. When choosing the appropriate method for your specific requirements, consider the following factors:
- Complexity: If your substring extraction task is simple, prefer built-in methods like String.substring(), String.subSequence(), and String.split(). Regular expressions can be more powerful but can also be more complex and slower for simple tasks.
- Flexibility: Regular expressions offer a higher degree of flexibility for pattern matching and substring extraction. If you need to extract substrings based on complex patterns, regular expressions might be the right choice.
- Memory and Performance: Be mindful of the memory and performance implications of each method, especially when working with large strings or performing many substring operations. Consider using techniques like String.intern(), StringBuilder, and precompiled regular expressions to optimize memory usage and performance.
By understanding the strengths and weaknesses of each method, you can choose the most appropriate approach for your specific substring extraction needs in Java.
I hope you found this article helpful.

Cheers!
Happy Coding.
About the Author
This article was authored by Rawnak.
1 thought on “Java Substring Extraction: Methods & Best Practices”
Comments are closed.