Updated March 24, 2023
Introduction to substring() Function in Java
Java is one of the most widely known and used Programming Language, provides a wide range of functions and methods. One of the main aspects of any programming language is the list of data types available. Java classifies data types into Primitive and Non-Primitive Data Types, where String is a Non-Primitive Data Type, as it refers to an object. This String object has various methods to perform various operations on the string. One of such methods is the substring() function in java.
substring(): A Part of a string is a substring. And the method substring() returns the part of the specified string. A substring method returns a new string that is a substring of this string.
Now that we have learned what a string and substring method is let’s understand the substring method’s standard syntax.
Syntax:
public String substring(int beginIndex)
This will return a fresh new string, which is a substring of the string that was passed. Parameters it takes is a beginIndex, which specifies the index point from where to pick the substring(). Start from the character of the specified index up to the end of the string.
Exception: In case if a negative index is passed or any index which is larger than the original length of the string, “IndexOutOfBoundsException” is thrown.
How does substring() Function work in Java?
When the substring() function in java is called, it creates a new string, which is part of the original string based on the beginIndex parameter passed and returns it. When the beginIndex is passed, it searches for the specified index value within the string and then picks the character present at the specified index. In one version, beginIndex is passed, and in another version of the substring method, beginIndex and endIndex are provided. It is important to understand the fact that every time a substring method is called, it will create a new string because the string data type is immutable.
Examples to Implement substring() Function
Let us see the examples to implement substring() function in java given below:
Example #1
Code:
public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is Java substring()");
System.out.print("The original substring: " +sampleStr);
System.out.print("\n Our new substring is : ");
System.out.println(sampleStr.substring(8));
}
}
Code Explanation: Our first sample program has a simple demonstrated substring method with the start index parameter. We created our class, then added our main class. Declared our String data type along with its variable name and the value. At first, we will print the original string as it is. Then we will print the new string with the effect of the substring method. We have specified the “beginIndex” to be from the 8th position, meaning our new substring will start from the 8th index, picking the character from the 8th position of the string up to the end. Upon executing the above code, the substring is expected to be “Java substring()”. Refer to the below attached screenshot for output.
Output:
As you can see, we have printed the original string, as it is, at first, and then the substring.
Example #2
We will now see another example, similar to the first one but with added parameter, “endIndex”.
Code:
public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is Java substring() and something extra.");
System.out.print("The original substring: " +sampleStr);
System.out.print("\n Our new substring: ");
System.out.println(sampleStr.substring(8, 24));
}
}
Code Explanation: Similar to the earlier code, we have our class and main class within. Then we have declared our string, with the variable name and the value, which is quite long compared to earlier. Next, we have printed our original string and then the new string. With our substring method, we have passed two parameters, beginIndex and endIndex, 8 and 24. These indexes define from which point the substring must start and where shall it end. In our example, from the 8th index, substring will begin, picking any character that is at the 8th index, up to the 24th index. So our new substring will be between the 8th and 24th index, prior to the 8th and after the 24th index. Upon executing the above code, the substring is expected to be “Java substring()”.
Output:
As you can see in the output, prior to 8th and characters later 24th position is not part of our newly created substring. One of the most widely used substring methods is to clean out unnecessary characters from the beginning and trailing.
Example #3
Code:
public class java_subs {
public static void main(String args[]) {
String sampleStr = new String("This is just another example of java substring.");
System.out.print("The original substring: " +sampleStr);
int slength = sampleStr.length();
System.out.print("\n Our new substring: ");
System.out.println(sampleStr.substring(2, slength-9));
}
}
Code Explanation: In our third example, we have our class and main class within. We have a slightly different string here, and we have used the length method. Similar to earlier examples, we have printed our original string and then declared a new integer with slength, and the value is the length of the string. Then in our final print statement, our beginIndex is 2, meaning the substring will begin from the 2nd index, and the endIndex is slength -9, meaning whatever the length of the string is, the substring will end on 9 minus the original length. In our example, slength -9 will result in a substring to end on “s”.
Output:
One of the important aspects to remember is, the original string is very long and holds the 1Gb size of an array. So, even if the substring being very small, it will be of 1GB. Though this will obviously stop the original string from being garbage collected, it is a case of memory leak, meaning a large amount of memory is acquired even though it won’t be used.
Conclusion
The string is one of the important data types, and the substring method simply returns a part of the original string. When the substring method is called, it creates a new string with an offset and count. We have demonstrated multiple examples with various methods to learn about substring. Code with its respective screenshots are attached.
Recommended Articles
This is a guide to substring() Function in Java. Here we discuss syntax and working of substring() Function in Java along with examples and its code implementation. You can also go through our other related articles to learn more –