substring() Method
Tags: java, javascript, beginners, discuss
Published: 2021-08-08 10:20:55

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](https://dev.to/ritvikdubey27/string-class-in-java-4jb7). 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](https://dev.to/ritvikdubey27/string-class-in-java-4jb7).

### 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

```Java
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);
	}
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-substring.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628414080767/ZVX1v35z6.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 -

```Java
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));
	}
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

And the output will be same -

![java-substring.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628414080767/ZVX1v35z6.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.**

```Java
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));
	}
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-substring(int startingIndex).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628414737509/a4O76xngr.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.**

```Java
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));
	}
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-substring(int startingIndex, int endingIndex).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628416003899/3Bnu9Y6oP.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()

```Java
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));
	}
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-substring().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628416757157/KdKFOLtAX.png)

### Printing a null string using substring()

```Java
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));
	}
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-substring(0, 0,).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628417020648/eCPLoopLy.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.**

```Java
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));
	}
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-substring(0, 0,).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628417020648/eCPLoopLy.png)

**Output is same in both the cases.**

![yay.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628417546083/JY5fgSjke.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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*