The substring() method in Java is used to extract a portion of a string. It is part of the String class and returns a new string that is a subset of the original string.

Syntax

Java provides two overloaded versions of the substring() method:

  • public String substring(int beginIndex)
    Returns a substring starting from beginIndex (inclusive) to the end of the string.

  • public String substring(int beginIndex, int endIndex)
    Returns a substring from beginIndex (inclusive) to endIndex (exclusive).

Key Points

  • Indexing starts at 0.

  • The beginIndex is inclusive, while the endIndex is exclusive.

  • The method always returns a new string; the original string remains unchanged because String is immutable in Java.

  • If indices are invalid, it throws a StringIndexOutOfBoundsException.

  • If the string is null, it throws a NullPointerException.

Examples

String str = "Hello, World!";

// Extract from index 7 to end
String part1 = str.substring(7); // "World!"

// Extract from index 0 to 5 (exclusive of index 5)
String part2 = str.substring(0, 5); // "Hello"

// Extract domain from URL
String url = "https://www.digitalocean.com";
String domain = url.substring(8, url.indexOf("/", 8)); // "www.digitalocean.com"

Common Use Cases

  • Extracting file extensions: filename.substring(filename.lastIndexOf(".") + 1)

  • Parsing dates: date.substring(0, 4) for year.

  • Extracting parts of URLs or email addresses.

  • Checking for palindromes or comparing string segments.

Best Practices

  • Always validate beginIndex and endIndex to avoid exceptions.

  • Check for null strings before calling substring().

  • Be mindful of memory usage: in older Java versions (pre-7), substrings shared the underlying character array, potentially causing memory leaks if large strings were retained.

For more details, refer to the Java documentation.

It's supposed to be an efficiency measure. i.e. when you're taking a substring you won't create a new char array, but merely create a window onto the existing char array.

Is this worthwhile ? Maybe. The downside is that it causes some confusion (e.g. see this SO question), plus each String object needs to carry the offset info into the array, even if it's not used.

EDIT: This behaviour has now changed as of Java 7. See the linked answer for more info

Answer from Brian Agnew on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The substring() method returns a substring from the string.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - The class String includes methods ... for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - In Java, the substring() method of the String class returns a substring from the given string.
🌐
Career Karma
careerkarma.com › blog › java › java substring: a how-to guide
Java Substring: A How-To Guide | Career Karma
December 1, 2023 - 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.
🌐
IronPDF
ironpdf.com › ironpdf for java › ironpdf for java blog › java help › java substring method
Java Substring Method (How It Works For Developers)
November 18, 2025 - The Java string substring method is part of the String class in Java. It's designed to create a new string from a portion of an existing string
🌐
CodingBat
codingbat.com › java › Warmup-2
CodingBat Java Warmup-2
Java Substring v2 (video) Java String Equals and Loops · Java String indexOf and Parsing · Java If and Boolean Logic · If Boolean Logic Example Solution Code 1 (video) If Boolean Logic Example Solution Code 2 (video) Java For and While Loops · Java Arrays and Loops ·
Find elsewhere
🌐
Edureka
edureka.co › blog › substring-in-java
Substring in Java | String substring() Method Example | Edureka
June 14, 2021 - This article on Substring in Java helps you learn about the syntax, different methods under substring with the help of examples
🌐
The Renegade Coder
therenegadecoder.com › code › be-careful-with-strings-substring-method-in-java
Be Careful with String’s Substring Method in Java – The Renegade Coder
May 26, 2020 - Returns a new string that is a substring of this string. 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. Java API, 2019
🌐
Quora
quora.com › How-do-you-use-substring-and-lastIndexOf-in-Java
How to use substring and lastIndexOf in Java - Quora
Answer: The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. The substring() method extracts characters from start to end (exclusive).
🌐
HappyCoders.eu
happycoders.eu › java › substring
Java substring() Method
June 12, 2025 - String string = "the quick brown fox jumps over the lazy dog"; string = string.replace("the", "a");Code language: Java (java) To remove a substring, we can replace it with the empty string "".
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java string substring method
Java String substring Method | PrepInsta
July 29, 2023 - To know more about String Class in java read the complete article. The substring() method is a member of the String class in Java and is used to extract a portion of a string.
🌐
Codedamn
codedamn.com › news › java
substring Method in Java (with examples)
October 15, 2022 - substring() method is a part of the Java String class and returns part of the string.
🌐
FavTutor
favtutor.com › blogs › java-substrings
Substring in Java (with examples)
November 5, 2023 - In Java, the substring is a powerful method that allows developers to extract subsets or parts of a string. By using the substring() method, you can create smaller strings from a bigger one without modifying the original string.
🌐
CodingBat
codingbat.com › doc › java-string-substring.html
Java Substring v2
There is a more complex version of substring() that takes both start and end index numbers: substring(int start, int end) returns a string of the chars beginning at the start index number and running up to but not including the end index.
🌐
DevGenius
blog.devgenius.io › java-substring-a-comprehensive-guide-082192995d1e
Java Substring: A Comprehensive Guide | Dev Genius
June 27, 2025 - A substring is a sequence of characters derived from another string. For example, in the string "Hello, World!", the substrings "Hello", "World", and "lo, Wo" are all valid substrings.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-substring-method-example
Java String substring() Method with examples
Note: The method is called like this: substring(start + 1, end), here start index is +1 because, we want to get the substring after the delimiter position, however the end index is not +1 because end index is not inclusive in substring method. public class JavaExample{ public static void main(String[] args){ //given string String str = "Welcome to [BeginnersBook.com]"; //we would like to get the substring between '[' and ']' int start = str.indexOf("["); int end = str.indexOf("]"); String outStr = str.substring(start + 1, end); System.out.println(outStr); } }
🌐
Code with Mosh
forum.codewithmosh.com › t › java-strings-substrings › 15036
Java Strings & Substrings - Code with Mosh Forum
September 9, 2022 - Can anyone pls help me to understand ... String A=sc.next(); String B=sc.next(); System.out.print(A.substring(0,1).toUpperCase()+ A.substring(1) + " "+B.substring(0,1).toUpperCase() + ......
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.substring
String.Substring Method (Java.Lang) | Microsoft Learn
Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.