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);
}
}
You can run your code online here
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));
}
}
You can run your code online here
And the output will be same -
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));
}
}
You can run your code online here
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));
}
}
You can run your code online here
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));
}
}
You can run your code online here
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));
}
}
You can run your code online here
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));
}
}
You can run your code online here
Output is same in both the cases.