DEV Community

Cover image for substring() Method
Ritvik Dubey
Ritvik Dubey

Posted on

substring() Method

Hello All👋 I hope you all are doing well. I have published a few articles related to String in java, you can check them here. Lately I realized that I never wrote about substring. So, this is a short article about substring in Java.

Let's begin...

First, what is a String?

String is a collection of characters in sequence. In programming it is used to represent text rather than numbers. Numbers can also be considered as a string if they are specified correctly. To represent a string we enclose it in within quotes. To read more about string click here.

What is a substring?

So now we know what is a String let's learn about substring, in very short substring is a part of String or substring is a continuous sequence of characters in a string. It is a built-in method of Java String class. This method returns some part of the given String. Thus the return type of this method in Java is a String.

For example :-

Let's take a String -

"I love DEV community"

Now there can be many substrings here, few of them are -

"love" , "DEV community", "unity", "i" etc.

So a part of the given string is called substring, even a single character from the string can be printed using substring().

Let's look at some code

public class Demo {
    public static void main(String[] args) {
        String str = "I love DEV community";
        System.out.println("Original string is : " + str);
        String subStr = "";
        subStr = str.substring(7);
        System.out.println("Substring is : " + subStr);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-substring.png

For your better understanding in above example I have written a little lengthy code.

Same code could be cut short to this, you don't need to declare a variable for substring always -

public class Demo {
    public static void main(String[] args) {
        String str = "I love DEV community";
        System.out.println("Original string is : " + str);
        System.out.println("Substring is : " + str.substring(7));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

And the output will be same -

java-substring.png

Let's look at the two variants of substring()

1. substring(int startingIndex);

The above example is actually the example of this variant, in this variant we specify the starting index from which index we want to make a substring. This variant returns the substring that starts at the given index and then runs through the entire original String.

Let's look at a different example.

public class Demo {
    public static void main(String[] args) {
        String str = "Java is my favorite programming language";
        System.out.println("Original string is : " + str);
        System.out.println("Substring is : " + str.substring(7));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-substring(int startingIndex).png

2. substring(int startingIndex, int endingIndex);

In this variant we will take two inputs, first, starting index, this is the value of beginning index, inclusive and second the ending index, this is the value of ending index, exclusive, meaning the substring ends at the ending index – 1 index.

Let's look at its example.

public class Demo {
    public static void main(String[] args) {
        String str = "I love coding";
        System.out.println("Original string is : " + str);
        System.out.println("Substring is : " + str.substring(2, 6));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-substring(int startingIndex, int endingIndex).png

Remember the index starts from 0.

There can be a exception thrown, IndexOutOfBoundException, if the value of starting index is greater than the length of the entire String or less than 0 or if starting index is greater than ending index and if the ending index is greater than the length of String.

Printing a single character using substring()

public class Demo {
    public static void main(String[] args) {
        String str = "I love coding";
        System.out.println("Original string is : " + str);
        System.out.println("Substring is : " + str.substring(4, 5));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-substring().png

Printing a null string using substring()

public class Demo {
    public static void main(String[] args) {
        String str = "I love coding";
        System.out.println("Original string is : " + str);
        System.out.println("Substring is : " + str.substring(0, 0));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-substring(0, 0,).png

This is not necessary if you pass starting index and ending index as 0 to print a null string, if there is 0 difference between both the index then also the output will be null.

public class Demo {
    public static void main(String[] args) {
        String str = "I love coding";
        System.out.println("Original string is : " + str);
        System.out.println("Substring is : " + str.substring(2, 2));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-substring(0, 0,).png

Output is same in both the cases.

yay.png

Okay so that's enough for now follow my this journey to learn more about Java.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Top comments (0)