Home > Java > javaTutorial > body text

substring() Function in Java

王林
Release: 2024-08-30 15:35:52
Original
818 people have browsed it

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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)
Copy after login

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));
}
}
Copy after login

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:

substring() Function in Java

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));
}
}
Copy after login

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:

substring() Function in Java

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));
}
}
Copy after login

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:

substring() Function in Java

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.

The above is the detailed content of substring() Function in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!