CodeGym /Java Blog /Strings in Java /Substring in Java
John Selawsky
Senior Java Developer and Tutor at LearningTree

Substring in Java

Published in the Strings in Java group
The most popular actions on String in Java are concatenation, getting a character by index and getting a substring. In this article we are going to tell you about Java substring method.

What is Java Substring?

Java Substring in general is a contiguous sequence of characters inside the String. It could be a part of the String or the whole String as well. Substring in Java - 2What is substring in Java? This language has a substring() method, or rather two methods, thanks to Java method overloading. You may use them to get substring in Java program. First Java substring method is String substring(firstIndex) and the second one is String substring (firstIndex, lastIndex).

How to use substring in Java

The answer is simple: just use the substring. Java substring() method returns a part of the string. There are two methods you can use for this:
Syntax of the substring method String substring(firstIndex) String substring (firstIndex, lastIndex)
Arguments firstIndex is a number in your String that is the first index of your substring. Inclusive. The last number of your Substring is the last number of whole string firstIndex is a number in your String that is the first index of your substring. Inclusive.
lastIndex is the number of your String, first and all after it are excluded from your substring
Examples of how to use substring in Java

String s = "CodeGym";
//returns Gym

String s = "CodeGym"; 
//returns deG
Very popular tasks that help you to understand
  • How to get a substring in Java
  • How to find all substrings of a given string
  • How to find the longest common substring

How to get a substring in Java (particular)

This first Java substring example is pretty easy. You've got a String and you need to find a substring "CodeGym" in it. You’ve already known how to get a substring in Java. So here is the solution of this particular problem:


public class Main {

   public static void main(String[] args) throws IOException {

       String s1 = "the best Java Core course is CourseCodeGym.  End of story";
       String myTarget = "CodeGym";
       int index1 = s1.indexOf(myTarget);
       int index2 = index1 + myTarget.length();
       System.out.println(s1.substring(index1, index2));

The output is: CodeGym Process finished with exit code 0 How to find all substrings of a given string Here we have the simplest way to find all substrings of a given String.

public class Main {
   public static void main(String[] args) throws IOException {
       String myTarget = "CodeGym";
       for (int i = 0; i < myTarget.length(); i++) {
           for (int j = i + 1; j <= myTarget.length(); j++) {
               System.out.println(myTarget.substring(i, j));
The output is: C Co Cod Code CodeG CodeGy CodeGym o od ode odeG odeGy odeGym d de deG deGy deGym e eG eGy eGym G Gy Gym y ym m Process finished with exit code 0 How to find the longest common substring The longest common substring problem is one of the most popular tasks in computer science. You can meet it on your Junior Developer interview with a pretty high probability. Anyway, try to solve it, it is a very useful exercise for a beginner programmer. The longest common substring problem means to find the longest string (or a few of them) that is a substring (or are substrings) of two or more strings. For example you have two strings

String first = "CodeGym" 
String second = "SomeGym"
Output should be: eGym So, you have to strings "first" and "second". Print the longest common substring. If two or more substrings have the same value for longest common substring, print any of them. We strongly recommend you to try solve this problem by yourself and only after that look into the code below.

public class SubStringTest {

   //  in this method we are looking for the Longest common substring of
   // first String with length = m  and the second String with length = n
   public static String longestCS(String first, String second, int m, int n) {
       // the maximal length
       int maxLength = 0;
       // the last index of the longest common substring
       int endIndex = m;

       // array stores the length of substring
       int[][] keeper = new int[m + 1][n + 1];

       for (int i = 1; i <= m; i++) {
           for (int j = 1; j <= n; j++) {
               // check if current characters of first and second strings match
               if (first.charAt(i - 1) == second.charAt(j - 1)) {
                   keeper[i][j] = keeper[i - 1][j - 1] + 1;
                   if (keeper[i][j] > maxLength) {
                       maxLength = keeper[i][j];
                       endIndex = i;
       return first.substring(endIndex - maxLength, endIndex);

   public static void main(String[] args) {
       String first = "CodeGym";
       String second = "SomeGym";
       int m = first.length(), n = second.length();
       System.out.println("The longest common substring = " + longestCS(first, second, m, n));
The output is: The longest common substring = eGym

How does substring work in Java

In JDK 7 and newer versions, substring() no longer counts the number of characters in the character array it creates, as it did in versions prior to JDK 6 inclusive, but simply creates a new array in memory (heap) and refers to it. Here is an example:

String x = "CodeGymIsTheBest";
String y = x.substring (2,6);
String z = x.substring (0,3);
So, in JDK 7 and later, objects y and z created as a result of the substring() method applied to object x will refer to two newly created arrays (on the heap) - {d,e, G, y} for y and {C, o} for z. In JDK 7 + version of the method substring, these two new lines (that is, two new character arrays) will be stored in memory along with the original string myLongString ({C, o, d, e, G, y, m, i, s, t, h, e, b,e,s,t} in the form of an array).

JDK 6 version of substring

Even today on some big projects you may meet legacy code base from the JDK 6 times. In JDK 6 method substring() works in a different way. As you probably know, String is an immutable Class and to get the substring Java used this immutability earlier in JDK 6. The Object of type String inside is just an array of characters, or rather, contains an array of characters. At the time of JDK 6, two more variables were stored there: the number of the first character in the character array and their quantity. Thus, in JDK 6, String had three fields of char value [] (character array), int offset (index of the first character in the array), and int count (the number of characters in the array). When a substring is created in JDK 6, the array of characters is not copied into the new String object. Instead of this, both Objects store a reference to the same Character Array. However the second Object stores two more variables, the first symbol of substring and the second is about how many symbols are in substring. The JDK 6 method was substituted because of memory leak problem. What does it mean? Let's assume we have a string x, and we create a couple of substrings using substring.

String x = "CodeGymIsTheBest";
String y = x.substring (2,6);
String z = x.substring (0,3);
Now we have an object x stored in a special area of ​​memory called the heap, and two objects y and z referring to the same object x. Only x refers to the elements from the second to the sixth, and z refers to the x elements from zero to third. What if the original object x has already been used and left without any references on it? In a program all other objects work with y and z only. In this case the garbage collector may destroy the object x, while the memory array remains, and it is used by y and z. The memory leak happens. So, the newer, JDK 7 and later version, method is pretty costly in terms of memory using. However it allows you to prevent the memory leak. In addition, the new method works faster, since it does not have to calculate the number of characters. To reinforce what you learned, we suggest you watch a video lesson from our Java Course