The substring() method in Java is a powerful tool for extracting parts of a string. This method always returns a new string, and the original string remains unchanged because String is immutable in Java.
In this tutorial, we’ll cover its syntax, use cases, and potential pitfalls while providing practical examples and solutions to common errors.
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
Parameters:
beginIndex
- the starting index (inclusive).
endIndex
(optional) - the ending index (exclusive).
Java String substring method is overloaded and has two variants.
substring(int beginIndex)
: This method returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.substring(int beginIndex, int endIndex)
: The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is (endIndex - beginIndex).IndexOutOfBoundsException
if any of the below conditions met.
substring()
ExampleHere is a simple program for the substring in java.
package com.journaldev.util;
public class StringSubstringExample {
public static void main(String[] args) {
String str = "www.journaldev.com";
System.out.println("Last 4 char String: " + str.substring(str.length() - 4));
System.out.println("First 4 char String: " + str.substring(0, 4));
System.out.println("website name: " + str.substring(4, 14));
}
}
Output of the above substring example program is:
Last 4 char String: .com
First 4 char String: www.
website name: journaldev
// Define a string
String str = "Hello, DigitalOcean!";
// Use the substring method to extract a part of the string
// The substring begins at the specified beginIndex (inclusive) and extends to the character at index endIndex - 1 (exclusive)
String subStr = str.substring(7, 18);
// Print the extracted substring
System.out.println(subStr); // Output: DigitalOcean
// This code block demonstrates how to extract the first three characters from each string in a list of words and store them in a new list.
// First, a list of words is defined using Arrays.asList.
List<String> words = Arrays.asList("apple", "banana", "cherry");
// The stream() method is used to create a stream from the list of words.
// The map() method is then used to transform each word in the stream into a new string that contains only the first three characters of the original word.
// This is achieved by calling the substring(0, 3) method on each word, which returns a new string starting from the first character (index 0) up to but not including the fourth character (index 3).
// Finally, the collect() method is used to collect the results of the mapping operation into a new list of strings.
List<String> substrings = words.stream()
.map(word -> word.substring(0, 3))
.collect(Collectors.toList());
// The resulting list of substrings is then printed to the console.
System.out.println(substrings); // Output: [app, ban, che]
// The output shows that the first three characters of each word have been successfully extracted and stored in a new list.
We can use the substring()
method to check if a String is a palindrome or not.
package com.journaldev.util;
public class StringPalindromeTest {
public static void main(String[] args) {
System.out.println(checkPalindrome("abcba"));
System.out.println(checkPalindrome("XYyx"));
System.out.println(checkPalindrome("871232178"));
System.out.println(checkPalindrome("CCCCC"));
}
private static boolean checkPalindrome(String str) {
if (str == null)
return false;
if (str.length() <= 1) {
return true;
}
String first = str.substring(0, 1);
String last = str.substring(str.length() - 1);
if (!first.equals(last))
return false;
else
return checkPalindrome(str.substring(1, str.length() - 1));
}
}
Here we are checking if the first letter and the last letter is the same or not. If they are not the same, return false. Otherwise, call the method again recursively passing the substring with the first and last letter removed.
You can checkout more string examples from our GitHub Repository.
Here is a table comparing various use cases of the substring() method.
Use Case | Description | Example |
---|---|---|
Extracting a part of a string | Extracts a part of a string based on start and end indices. | String part = "substringExample".substring(0, 10); |
Validating a string prefix | Checks if a string starts with a specific prefix. | if ("substringExample".substring(0, 3).equals("sub")) { ... } |
Validating a string suffix | Checks if a string ends with a specific suffix. | if ("substringExample".substring("substringExample".length() - 3).equals("ple")) { ... } |
Checking for a palindrome | Checks if a string is the same forwards and backwards. | boolean isPalindrome = "madam".substring(0, 1).equals("madam".substring("madam".length() - 1)); |
StringIndexOutOfBoundsException
ErrorsThe substring()
method throws a StringIndexOutOfBoundsException
if:
The beginIndex
is negative.
The endIndex
is greater than the string length.
If the beginIndex
> endIndex
.
// This will throw StringIndexOutOfBoundsException because:
// String length=4, requested end index=6
String text = "Java";
try {
String sub = text.substring(2, 6);
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Invalid substring range: " + e.getMessage());
}
Solution: Always validate indices before calling substring()
.
if (beginIndex >= 0 && endIndex <= text.length() && beginIndex < endIndex) {
String sub = text.substring(beginIndex, endIndex);
}
substring()
with Null or Empty StringsString str = null;
try {
String result = str.substring(0, 2);
} catch (NullPointerException e) {
System.out.println("Cannot extract substring from null string.");
}
Solution: Check for null
before calling substring()
.
if (str != null) {
String result = str.substring(0, 2);
} else {
// Handle null case appropriately
System.out.println("Cannot extract substring from null string.");
result = ""; // or set default value
}
So, here is the final code block:
String str = null;
String result = "";
if (str != null && str.length() >= 2) {
result = str.substring(0, 2);
} else {
System.out.println("Cannot extract substring - null or too short");
}
System.out.println("Result: " + result);
Error Type | Cause | Solution |
---|---|---|
StringIndexOutOfBoundsException |
Using an index out of range | Validate index before calling substring() |
NullPointerException |
Calling substring() on null |
Check for null before calling substring() |
Performance Issues | Using substring() frequently in large strings |
Consider StringBuilder or StringBuffer |
substring()
String filename = "document.pdf";
String extension = filename.substring(filename.lastIndexOf(".") + 1);
System.out.println(extension); // Output: pdf
String url = "https://www.digitalocean.com/community/tutorials/java-string-substring";
String domain = url.substring(8, url.indexOf("/", 8));
System.out.println(domain); // Output: www.digitalocean.com
substring()
with split()
Feature | substring() | split() |
---|---|---|
Use Case | Extracts a part of a string | Splits a string into multiple parts |
Performance | Faster for simple extractions | Slower due to regex processing |
Returns | Substring | Array of substrings |
The substring()
method is a built-in function in Java’s String class. You can use string.substring(startIndex, endIndex)
to extract a portion of a string. Here’s an example code block to illustrate its usage:
String originalString = "Hello, World!";
String extractedSubstring = originalString.substring(7, 13); // Extracts "World!"
System.out.println("Extracted Substring: " + extractedSubstring);
// Output: Extracted Substring: World!
You can use Java Streams to extract substrings from a list:
List<String> words = Arrays.asList("apple", "banana", "cherry");
List<String> substrings = words.stream()
.map(word -> word.substring(0, 3))
.collect(Collectors.toList());
charAt()
in Java?The charAt(int index)
method in Java returns the character at a specified index in a string.
String word = "Java";
char letter = word.charAt(1);
System.out.println(letter); // Output: a
Use substring(beginIndex, endIndex)
:
String str = "Hello, World!";
String sub = str.substring(0, 5);
System.out.println(sub); // Output: Hello
The substring()
method is the best way to extract parts of a string. Here’s an example of how to use it:
String originalString = "Hello, World!";
String extractedSubstring = originalString.substring(7, 13); // Extracts "World!"
System.out.println("Extracted Substring: " + extractedSubstring);
// Output: Extracted Substring: World!
Another approach is using split()
if you need multiple parts. Here’s an example of how to use it:
String originalString = "Hello, World!";
String[] splitStrings = originalString.split(", "); // Splits into "Hello" and "World!"
System.out.println("Split String 1: " + splitStrings[0]);
System.out.println("Split String 2: " + splitStrings[1]);
// Output:
// Split String 1: Hello
// Split String 2: World!
substring()
and split()
in Java?The substring()
method extracts a single portion of a string, while split()
divides a string into an array based on a delimiter.
Yes, substring()
can throw a StringIndexOutOfBoundsException
if the indices are invalid. Always validate your indices before calling substring()
. Here’s an example of how to validate indices and avoid this exception:
String str = "Hello, World!";
int startIndex = 0;
int endIndex = 5; // Ensure endIndex is within the string length
// Validate indices before calling substring()
if (startIndex >= 0 && endIndex <= str.length()) {
String sub = str.substring(startIndex, endIndex);
System.out.println(sub); // Output: Hello
} else {
System.out.println("Invalid indices for substring operation.");
}
In conclusion, extracting parts of a string in Java is a crucial skill for any developer. The substring()
method is a powerful tool for extracting a single portion of a string, while split()
is useful for dividing a string into multiple parts based on a delimiter. It’s essential to understand the differences between these methods and how to use them effectively in your code.
Additionally, being aware of the potential for StringIndexOutOfBoundsException
when using substring()
and taking steps to validate indices can help prevent errors in your code. By mastering these techniques, you’ll be able to manipulate strings with ease and write more efficient, effective code.
For further reading, check out these related Java tutorials:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Hello Pankaj I want to understand negative indexing in strings, If it starts from -1 at the end of the string, it should go on -1,-2,-3 so on from right to left.what does 0th index represent then? I don’t understand the 3rd example here. I looked it up but haven’t found anything useful so far. ex: var str = ‘The morning is upon us.’; str.slice(-3); // returns ‘us.’ str.slice(-3, -1); // returns ‘us’ str.slice(0, -1); // returns 'The morning is upon us’1234
- Bindu Yadav