Java String substring() method with Example | Substring() Method in Java with or without End Index

In this tutorial, we will learn completely about the Java String substring() with Examples. Basically, there are two variants of the substring method is applied to find the substring in Java. So, stay continued to this page and understand how it returns in two variants. Also, you can understand clearly the substring() method in java after viewing our ultimate study tutorial.

Java String substring()

Generally, a substring is a subset of another string. In java, the substring() method returns a new string that is a substring of this string. Java String substring() method is an overloaded method.

Syntax:

substring(//any one of the methods)
{
//body
}

Parameters

  • beginIndex: beginning index is inclusive
  • endIndex: closing index is exclusive

substring() Return Value

Thesubstring()method returns a substring from the provided string.

java substring working image

String substring() Method Important Points

  1. In case, any of the below conditions satisfies then both the string substring methods can throw IndexOutOfBoundsException.
    • if the startIndex is negative
    • The length of this String object is smaller than the endIndex
    • startIndex is larger than endIndex
  2. beginIndex is inclusive and endIndex is exclusive in both substring methods.

Also Read:

String substring() method variants

We can see the two variants to use the java substring() method. Let’s discuss two of them in detail below-

1. When we pass only the starting index:

The first way of the substring(int startIndex) method, returns a substring from the specified index to the end of the string. However, if you require to return a string that lets you specify both the beginning and ending index of the substring, then we must go for the second way of the substring method.

Syntax: The following is the syntax for the first variant of the String substring() method in java:

String substring(int beginIndex)

Example of Java substring() Without End Index

class Main {
public static void main(String[] args) {
String str1 = "program";

// from the first character to the end
System.out.println(str1.substring(0)); // program

// from the 4th character to the end
System.out.println(str1.substring(3)); // gram
}
}

2. When we pass both the indexes, starting index and end index:

The second variant of the substring(int startIndex, int endIndex) method returns a new string that is a substring of this string. The substring starts at the specified startIndex and continues to the character at index endIndex -1. Similar to the first way of the substring(int startIndex) method, it throws StringIndexOutOfBoundsException if the startIndex is negative, or endIndex is larger than the length of this String object, or startIndex is larger than endIndex.

Syntax: The syntax of the second way of the java substring method is provided below:

public String substring(int startIndex, int endIndex)

Here, startIndex defines the beginning index, and the endIndex defines the stopping point.

Java substring() Method Example with End Index

class SubString{
public static void main(String args[]){
String str = "String Tutorial by Btechgeeks is best";
//returns a new string from specified
//starting index to ending index.
//returns Tutorial by Javastudypoint
System.out.println(str.substring(7,30));
}
}

Output:

Tutorial by Bteckgeeks

Note: Remember that it throws StringIndexOutOfBoundsException if the startIndex is negative, or endIndex is larger than the length of this String object, or startIndex is larger than endIndex.

Let’s see the example:

class SubString{
public static void main(String args[]){
String str = "String Tutorial by btechgeeks is best";
//throws exception because in this case
//starting index is larger than
//ending index.
System.out.println(str.substring(33,7));
}
}

Output:

Java String substring() method with Example