Java String substring() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, substring() returns a new String that contains part of the original text. You give the start index, and optionally the end index; the character at the end index is not included.

Indexes are zero-based, so "Hello, Java!".substring(7, 11) is "Java". The length of the result is always endIndex - beginIndex, which makes fixed-width fields such as dates easy to cut out.

substring() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.substring(...); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result.

Syntax

Pass a start index to keep everything to the end, or a start and an end index for a slice.

java
String tail  = text.substring(7);      // from index 7 to the end
String slice = text.substring(0, 5);   // indexes 0..4
String year  = date.substring(0, 4);   // "2026" from "2026-11-19"
CallWhat it does
"Hello".substring(1)"ello": drops the first char.
"Hello".substring(1, 3)"el": indexes 1 and 2.
"Hello".substring(5)"": starting at the length is allowed.
"Hello".substring(0, "Hello".length())The whole text.

Think of the indexes as the gaps between characters: substring(1, 3) cuts at gap 1 and gap 3. The end index is exclusive, so s.substring(i, i) is always an empty string.

Library declarations
java
public String substring(int beginIndex);
public String substring(int beginIndex, int endIndex);

Parameters

ParameterDescription
beginIndexFirst index to include (0 to length()).
endIndexOptional. Index to stop before (exclusive). Defaults to length().

Return Value

Return ValueDescription
StringThe characters from beginIndex to endIndex - 1. The original string is unchanged.

Java Examples

Example 1 — Cut Text with One or Two Indexes

Take the tail of a greeting, its first word, and a word from the middle, then show that the original is untouched.

java
public class SubstringBasics {
    public static void main(String[] args) {
        String text = "Hello, Java!";

        System.out.println(text.substring(7));
        System.out.println(text.substring(0, 5));
        System.out.println(text.substring(7, 11));
        System.out.println("original: " + text);
    }
}

How It Works

  1. One argument keeps the tail. substring(7) starts at the 'J' and runs to the end, giving "Java!".
  2. The end index is left out. substring(0, 5) returns indexes 0 to 4 ("Hello"), and substring(7, 11) stops before the '!' at 11.
  3. Immutable source. Each call built a new String; printing text again shows "Hello, Java!" exactly as before.

Example 2 — Read the Fields of a Fixed-Format Date

An ISO date always has the same layout, so fixed index ranges pick out the year, month, and day.

java
public class SubstringDate {
    public static void main(String[] args) {
        String date = "2026-11-19";

        String year = date.substring(0, 4);
        String month = date.substring(5, 7);
        String day = date.substring(8);

        System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day);
        int nextMonth = Integer.parseInt(month) + 1;
        System.out.println("Next month: " + nextMonth);
    }
}

How It Works

  1. Skip the dashes. The year covers indexes 0-3, the month 5-6, and the day 8-9; the dashes at 4 and 7 fall between the ranges.
  2. Length equals end minus start. substring(5, 7) returns exactly 2 characters, "11", and substring(8) takes the last two.
  3. Slices are still text. To do arithmetic, convert with Integer.parseInt("11"); adding 1 then prints 12.

Example 3 — Empty Results and Out-of-Range Indexes

Find out which index combinations are legal and which throw an exception.

java
public class SubstringEdges {
    public static void main(String[] args) {
        String word = "Code";

        System.out.println("[" + word.substring(4) + "]");
        System.out.println("[" + word.substring(1, 1) + "]");

        try {
            System.out.println(word.substring(1, 6));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("substring(1, 6) -> " + e.getClass().getSimpleName());
        }
        try {
            System.out.println(word.substring(3, 1));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("substring(3, 1) -> " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Start at the length. "Code" has length 4, so substring(4) is legal and returns an empty string, shown as [].
  2. Equal indexes. substring(1, 1) selects zero characters, again an empty string rather than an error.
  3. Illegal ranges throw. An end of 6 is past the length, and a start of 3 after an end of 1 is backwards; both raise StringIndexOutOfBoundsException.

Frequently Asked Questions

No. The end index is exclusive: substring(0, 5) returns the characters at indexes 0, 1, 2, 3, and 4.
An empty string. substring(i, i) is legal for any i from 0 to length().
beginIndex is negative, endIndex is larger than length(), or beginIndex is larger than endIndex. Often an indexOf() result of -1 was used without a check.
No. Strings are immutable. substring() returns a new String and the original keeps all its characters.
They return the same characters. substring() is declared to return String, subSequence() is declared to return CharSequence so String can implement the CharSequence interface.

Next: toCharArray()

Copy the characters into a brand-new char array.

toCharArray() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful