Using the Substring Method in Java

To get a part of a string in Java, use the substring() method of the String class. substring(beginIndex) returns the characters from beginIndex to the end of the string, and substring(beginIndex, endIndex) stops before endIndex. Indexes start at 0. In this Java Substring Example, we take two substrings of the word Programming. Click Execute to run the Java Substring Example online and see the result.
Using the Substring Method in Java Execute
public class SubstringDemo {
    public static void main(String[] args) {
        String str = "Programming";

        String sub1 = str.substring(4);
        System.out.println("Substring from index 4: " + sub1);

        String sub2 = str.substring(4, 7);
        System.out.println("Substring from index 4 to 7: " + sub2);
    }
}

Java String substring() Syntax

substring() is a method of the String class. It has two forms: one takes a begin index, the other a begin index and an end index.

Java substring() Syntax
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)

Indexes start at 0. The begin index is inclusive and the end index is exclusive, so the substring has endIndex - beginIndex characters. substring() throws an IndexOutOfBoundsException if beginIndex is negative, endIndex is larger than the string length, or beginIndex is larger than endIndex.

Java Substring Examples

The following are examples of getting a substring in Java. Run them in the online Java code runner and change the indexes to see how the result changes.

Get a substring from an index to the end

With one argument, substring() returns the characters from the begin index to the end of the string.

Java substring(beginIndex) Example
public class SubstringBeginExample {
    public static void main(String[] args) {
        String word = "unhappy";
        System.out.println(word.substring(2));
    }
}

// output: happy

Get a substring between two indexes

With two arguments, substring() stops before the end index.

Java substring(beginIndex, endIndex) Example
public class SubstringRangeExample {
    public static void main(String[] args) {
        String word = "hamburger";
        System.out.println(word.substring(4, 8));
    }
}

// output: urge

Get the first character with substring(0, 1)

substring(0, 1) returns the first character as a String. To get it as a char, use charAt(0).

Java substring(0, 1) Example
public class FirstCharExample {
    public static void main(String[] args) {
        String language = "Java";
        String first = language.substring(0, 1);
        char firstChar = language.charAt(0);
        System.out.println(first + " " + firstChar);
    }
}

// output: J J

Get a substring before and after a character

To split a string at a character, find the character with indexOf() and pass its index to substring().

Java Substring After Character Example
public class SubstringAfterCharExample {
    public static void main(String[] args) {
        String email = "[email protected]";
        int at = email.indexOf('@');
        System.out.println(email.substring(0, at));
        System.out.println(email.substring(at + 1));
    }
}

// output:
// user
// example.com

See also

Updated: