Java String substring() method with examples

The Java String substring() method returns a part of the String and there are two overloaded substring() methods in Java.

Let’s look at both of the methods one by one

public String substring(int beginIndex)

What does it do? It takes an index into the argument from which it will start considering the string that it should return back. Remember here the indexing is 0-based indexing, which means if you want a substring that starts from the 2nd element from the string, then you have to pass 1 as an index into the function’s argument.
What does it return? It will return a string that is a part of the string that begins from the specified index.

So, let’s take a string “hello codekru” and now we only want the string “codekru” and now let’s try to have that string using the substring function.

"hello codekru" string

So, we have to put the beginIndex as 6 and the substring function will return the string “codekru” as shown in the below example

public class Codekru {

	public static void main(String[] args) {

		String str = "hello codekru";

		System.out.println("substring : " + str.substring(6));

	}
}

Output –

substring : codekru

There is also one important point to note here that if we pass index or beginIndex as 0 in the substring() function, then it will return the same string reference which we can confirm using the == operator, but if you pass any other index ( less than string length ), then it will return a new string reference which will be the substring of the string.

public String substring(int beginIndex, int endIndex)

What does it do? It takes two indexes – beginIndex and endIndex in the function’s argument.

  • beginIndex – This is the index from where to start ( inclusive ).
  • endIndex – This is the ending index where we want to stop ( exclusive ).

Here inclusive means that the beginIndex will be considered for substring and exclusive means that the endIndex will not be considered for the substring. So, effectively, we will be taking the substring from beginIndex to endIndex -1.

Let’s take the example of “hello codekru” string again but this time we want the string “code” returned back to us

"hello codekru" string

So, to have the string “code“, we have to pass the beginIndex as 6 and endIndex as 10.

public class Codekru {

	public static void main(String[] args) {

		String str = "hello codekru";

		System.out.println("substring : " + str.substring(6,10));

	}
}

Output –

substring : code

Here also, if we pass the beginIndex as 0 and endIndex as the string length, then the substring() function will return the same string reference otherwise it will return a new string which will be the substring of the specified string.

Now, a fun fact, do you know that substring(int beginIndex) internally calls the substring(int beginIndex, int endIndex) with endIndex = string length?

    public String substring(int beginIndex) {
        return substring(beginIndex, length());
    }

The What If scenarios

What if we try to put endIndex > String length in the substring() function’s argument?

Here we will get the StringIndexOutOfBoundsException as shown in the below program

public class Codekru {

	public static void main(String[] args) {

		String str1 = "hello codekru";

		String str2 = str1.substring(0, str1.length() + 1);

		System.out.println("substring: " + str2);

	}
}

Output –

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 14, length 13
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734)
	at java.base/java.lang.String.substring(String.java:1903)

What if we put beginIndex = 0 and endIndex = String length?

We also mentioned earlier in the article that in this scenario, it would return the same string reference but now we would prove it too.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "hello codekru";

		String str2 = str1.substring(0, str1.length());

		if (str1 == str2) {
			System.out.println("Both strings points to the same reference");
		} else {
			System.out.println("Both strings are pointing to the different references");
		}

	}
}

Output –

Both strings points to the same reference

What if we tried to use the substring() function on a null string?

In this case, you are going to get the infamous NullPointerException

public class Codekru {

	public static void main(String[] args) {

		String str1 = null;

		String str2 = str1.substring(0);

		System.out.println("substring: "+str2);

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.substring(int)" because "str1" is null

If you want to know more about strings and their methods, then we recommend giving this article a read.

Well, this is it. Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.