How to Get Substrings by Java substring Method

In this Infographic, the substring method of Java String class is explained with two code snippets. Main points about the Substring method is also covered.

What is Java substring method?

  • In the string class chapter of Java, we learned how to create strings along with a few useful methods of the String class.
  • One of the useful methods of the String class is substring().
  • The substring is part of the source string that you may get by using the substring() method.
  • In the Java substring method, you may specify the starting and end positions in the source string as integer values.
  • The position or index of a string starts at 0.
  • So, if you want to get the substring “tutorial” from the following string, this is how the substring() method can be used:
The source string: “This is Java substring tutorial!”

The substring method will be:

Str_substring.substring(23,31)
  • Where the Str_substring is the source string object.
  • The value 23 tells to start the returned substring from the letter “t” and end at 31 characters in the string.

Have a look at this demonstration in a complete Java program below.

An example of using substring Java method

Using the same string as mentioned above, we will get the substring “tutorial” as follows:

Java code:

public class String_demo {

    public static void main(String []args) {

        String Str_substring = "This is Java substring tutorial!";

        System.out.println("The substring is: " +  Str_substring.substring(23,31));

 }     
}

Output:

The substring is: tutorial

A demo of using only the starting index in the Java substring method

In the above example, I used both starting and end indices to get the substring. However, the end index is not required.

In that case, the returned substring will be the remaining characters including the begin Index character in the string.

Note that the left value is for the starting index that includes the character in the string.

The right value is the ending index position that does not include the character.

See this example where I used only the starting index for the same string as in the above example.

The code with starting index only:

public class String_demo {
    public static void main(String []args) {

        String Str_substring = "This is Java substring tutorial!";
        System.out.println("The substring is: " +  Str_substring.substring(8));

 }     

}

Output:

The substring is: Java substring tutorial!

What if the starting index is greater than the string length?

If you specify the starting index greater than the string length in the string’s substring method, an error will be generated. The error looks something like this:

Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1

See this example’s code where string length is 22. I used 23 starting index in the substring method.

The code:

public class String_demo {

    public static void main(String []args) {
        String Str_substring = "Strings are immutable.";

        System.out.println("Length of string: " +  Str_substring.length());
        System.out.println("The substring is: " +  Str_substring.substring(23));

 }     

}

Result:

substring error

So, as using the substring method beware of this exception.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!