Java is amazing, both the drink and the programming language, that is a point in fact. But I must digress because today we are only talking about the programming language. More specifically, we will discuss a string manipulation method called the substring method.
In any programming language, interacting with and manipulating strings is a very important task. Developers need to perform this task often, and as a result, the people that oversee the growth of a programming language have taken steps to make it easier. For Java, a great example of these efforts can be found in the substring method.
This post will go over what the substring method does, how it works, and how you can implement it into your work. Without any more delay, let’s get this party perking.
Java String API
In Java, string methods are also commonly referred to as string call APIs, and the two terms are considered interchangeable. To understand what this means, let’s quickly look at how Java handles strings.
Strings in Java are considered objects and are immutable which means they cannot be changed after creating them. New strings that get created are added to the Java constant pool, this is where all constants — immutable — objects are stored for use as needed.
When strings are referenced Java will check for them in the constant pool. If it is present, it will return an instance of it. The only time a string gets added to the constant pool is if a reference to one isn’t present in the pool.
Java String Substring
The Java substring method has two variations, the first requires a starting index, and the second variant uses the starting index and an ending index. Each version performs differently, but both return a new string that is a substring of the first.
This line of code calls on the public class String to access the substring method within it. This is where access to string methods comes from. Next, let’s look at some examples of the two variants of the substring method.
Java String Substring Start Index Integer
The substring method takes a single integer that identifies the starting index, with this variant of the method it will continue to the end of the string. You will need to start by creating a string before you can do anything with the substring method.
Let’s assume the above string for the remainder of this post, and use this to create a couple of substrings. With the first variant of the substring method, you can create a substring of “awesome!” using the following code.
The above code may look a little confusing, so let’s break it down a little further. First, this line of code accesses the print function from the System.out object. All this really does is provide the ability to print information to the console. Inside of the print method we call the substring method on the previously created string.
The substring method then searches the target string for a character at index 11, it then extracts a substring based on all the characters in the string beyond that index. This successfully creates a substring of “awesome!” as expected.
This is very powerful, but what if you want to create a substring from the beginning or middle of a larger string? Well, that would be where the second substring variant comes into play.
Java String Substring Start and End Index Integer
The second variant of the Java substring method takes two parameters, a start index and an end index. This is important because it allows us to dictate where the substring should stop extracting characters for the new string. Now that we have discussed how it works, let’s look at an example of it.
/* using substring() to extract substring*/
Final Takeaways on Using the Java String Substring Method
There isn’t much more to the Java String substring method and we’ve covered everything you need to know to get started Let’s recap the most important points to remember about how the method works.
- The substring method has two variant syntaxes
- The first version only accepts one parameter which is a start index for the method
- The second version accepts two parameters: the start and end indexes for the method.
- The start index for both versions is inclusive, which means it includes the character in that index location.
- The end index is non-inclusive and as such does not include the character at that index location.