Tutorial

Master Java Substring Method: Examples, Syntax, and Use Cases

Updated on February 20, 2025
Master Java Substring Method: Examples, Syntax, and Use Cases

Introduction

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.

Syntax of substring() Method

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() Methods

java string substring, substring in java Java String substring method is overloaded and has two variants.

  1. 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.
  2. 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).

String substring() Method Important Points

  1. Both the string substring methods can throw IndexOutOfBoundsException if any of the below conditions met.
    • if the beginIndex is negative
    • endIndex is larger than the length of this String object
    • beginIndex is larger than endIndex
  2. beginIndex is inclusive and endIndex is exclusive in both substring methods.

Java String substring() Example

Here 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

Example 1: Extracting a Part of a String

// 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

Example 2: Extracting from a List of Strings

// 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.

Checking Palindrome using substring() Method

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));

Common Errors and Debugging

Handling StringIndexOutOfBoundsException Errors

The substring() method throws a StringIndexOutOfBoundsException if:

  1. The beginIndex is negative.

  2. The endIndex is greater than the string length.

  3. If the beginIndex > endIndex.

Example of Error and Solution

// 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);
}

Misuse of substring() with Null or Empty Strings

Handling Null Strings

String 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

Some practical Use Cases of substring()

Extracting File Extensions

String filename = "document.pdf";
String extension = filename.substring(filename.lastIndexOf(".") + 1);
System.out.println(extension); // Output: pdf

Parsing URLs

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

Comparing 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

Frequently Asked Questions (FAQs)

1. How do you find the substring method in Java?

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!

2. How to get a substring from a list in Java?

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());

3. What is 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

4. How do I find the substring of a string?

Use substring(beginIndex, endIndex):

String str = "Hello, World!";
String sub = str.substring(0, 5);
System.out.println(sub); // Output: Hello

5. How do you extract part of a string in Java?

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!

6. What is the difference between 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.

7. Can the substring method throw an exception?

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.");
}

Conclusion

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:

  1. Java String Tutorial.
  2. Java Remove Character from String.
  3. String Programs in Java.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author


Default avatar

Sr Technical Writer

Senior Technical Writer @ DigitalOcean | 2x Medium Top Writers | 2 Million+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 13, 2017

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

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.