Java String substring() Method with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The substring() method in Java serves the purpose of extracting a portion of a String. It comes in two distinct variants. The first variant, String substring(int beginIndex), generates a new String that commences with the character at the specified beginIndex and continues to the end of the original String.
The second variant, String substring(int beginIndex, int endIndex), creates a new String starting at beginIndex and extending to the character at endIndex – 1.
In this article, we will delve into the syntax, parameters, and return values and provide examples for both forms of the substring() method. Additionally, we will explore the time and space complexities associated with this method and various common scenarios where substring extraction proves valuable.
Java String substring(int beginIndex)
The substring(int beginIndex) variant returns a new String that begins with the character at the specified beginIndex and extends to the end of the original String.
Syntax
public String substring(int beginIndex)
Parameters
beginIndex – The begin index, inclusive. This is the starting index of the substring.
Return Value
This method returns a new String containing the extracted substring.
Example
public class SubstringExample { public static void main(String[] args) { String s = "HelloWorld"; String sub = s.substring(5); System.out.println(sub); } }
Output:
World
With s.substring(5), the substring begins at index 5, which is “W,” and extends to the end of the string, resulting in “World” as the output.
Java String substring(int beginIndex, int endIndex)
The substring(int beginIndex, int endIndex) variant returns a new String that begins with the character at the specified beginIndex and extends to the character at index endIndex – 1.
Syntax
public String substring(int beginIndex, int endIndex)
Parameters
- beginIndex – The begin index, inclusive
- endIndex – The end index, exclusive
Return Value
This method returns a new String containing the extracted substring.
Example
public class SubstringExample { public static void main(String[] args) { String s = "HelloWorld"; String sub = s.substring(0, 5); System.out.println(sub); } }
Output:
Hello
In this code, we use the substring() method with two arguments: s.substring(0, 5). It extracts the substring starting at index 0 (inclusive) and ending at index 5 (exclusive), resulting in “Hello” as the output.
Complexity
Time Complexity: As we reference a part of the original String, the substring() method has a constant time complexity O(1).
Space Complexity: The substring() method creates a new String, therefore it has a space complexity of O(n) where n is the length of the substring.
Applications of substring() in Java
Substring extraction using substring() is helpful in many scenarios:
- Extracting prefixes or suffixes – When we want to extract a prefix or suffix from a string, e.g. extracting the domain from an email address.
- Parsing strings – Extracting relevant data from strings with fixed formats or mixed content. E.g. parsing log files.
Here is an example application to extract the domain from an email address:
public class EmailParser { public static String extractDomain(String email) { // Find @ index int atIdx = email.indexOf('@'); // Extract substring after @ String domain = email.substring(atIdx+1); return domain; } public static void main(String[] args) { String email = "[email protected]"; String domain = extractDomain(email); System.out.println(domain); } }
This code finds the index of ‘@’ using indexOf() and then extracts the substring after that index as the domain.
Output:
example.com
Conclusion
Java’s substring() method is a helpful String manipulation tool for extracting substrings from a larger String. Its two variants allow extracting a substring given just a starting index or both a start and end index.
Substring extraction has a constant time complexity, which makes it efficient even for large strings. The method has wide applicability for tasks like parsing strings, extracting affixes, and data cleaning. However, it does create a new String object, so it takes up additional memory proportional to the size of the extracted substring.
Overall, substring() is a simple yet powerful method for manipulating substrings in Java. Understanding how to use it correctly can help write cleaner and more efficient string processing code.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google