Java String substring()

The String substring() method returns a new string that is a substring of the given string.

substring() uses the start and optionally the end indices, passed as method arguments, to determine the substring position within the original string.

String substring(int beginIndex)
String substring​(int beginIndex, int endIndex)
String text = "Lorem Ipsum is simply dummy text";

String substring = text.substring(5);
String substring = text.substring(3, 10);

1. String substring() Method

The substring() method in the java.lang.String class is an overloaded method:

String substring(int beginIndex)
String substring​(int beginIndex, int endIndex)

Method Parameters

The substring() method arguments are:

  • beginIndex – the beginning index, inclusive.
  • endIndex – the ending index, exclusive.

Return Value

It returns the substring from the given string.

2. Find substring with the begin index

Supply only begin index when we need to find a substring starting from the given index position to the end of the string.

String substring(int beginIndex)

// Where 0 <= beginIndex <= Length of the String
  • It returns the substring starting from the specified index till the end of the given string.
  • indices range with 0 to N where N is length of string.
  • The begin index location is inclusive i.e. the result substring will contain the character at index position 'beginIndex'.
  • It throws IndexOutOfBoundsException if the beginIndex is less than zero or greater than the length of the given string (N).
  • If we pass the index value N (string lenth), an empty string is returned.

Example: Find substring with beginning index

In the given example, character 'e' is in the index position 3. The substring() includes the begin index in the output so the returned substring output starts from e till the end of the string.

String text = "Lorem Ipsum is simply dummy text";
String substring = text.substring(3);
System.out.println(substring);

Program output.

em Ipsum is simply dummy text

3. Find substring with the begin index and the end indices

Supply both indices (begin index and end index) when we need to find a substring starting from the given index position to the given end index position.

String substring(int beginIndex, int endIndex)

// Where: 
// 0 <= beginIndex <= Length of the String
// beginIndex <= endIndex <= N
  • It returns the substring starting from the specified index till the end index position.
  • indices range with 0 to N where N is length of string.
  • The begin index location is inclusive and end index position is exclusive. i.e. the result substring will contain the character at index position 'beginIndex' but the will not contain the character at index position.
  • It throws IndexOutOfBoundsException if the beginIndex is less than zero or greater than the length of the given string (N). Also, endIndex should be greater than or equal to beginIndex and less than the length of the given string (N).
  • If we pass the same index in both parameters then the empty string is returned.

Example: Find substring with beginning index and the end index

In the given example, character 'e' is in the begin index position 3 and 'm' is in the end index position 10.

The character in the begin index is included, while the character at the end index is excluded.

The length of the substring is equal to the difference between the end index and begin index positions.

String text = "Lorem Ipsum is simply dummy text";
String substring = text.substring(3, 10);
		
System.out.println(substring);
System.out.println(substring.length());

Program output.

em Ipsu
7

4. Blank spaces are also counted in substring

While calculating the index locations, please be aware of blank spaces. A blank space is also taken as one character in this string.

In the given example, we are getting the substring from index locations 3 to 8. At location 3, we have a blank space. This blank will also be part of the substring returned from the method.

String str = "The blog name is howtodoinjava.com";
        
System.out.println(str.substring(3, 8));    //<space>blog

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java String substring()”

  1. Hey. Nice article. A small suggestion : as mentioned in the article ” The substring begins with the character at the specified ‘beginIndex’ to the ‘endIndex’ position.” You should also clarify that the ‘endIndex’ is excluded. As in, it is a [) behavior.

    eg: in case of “hello world”.substring(6,9) “wor” is returned, and not “worl”.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.