Udemy logo

java string substringJava String class is one of the richest classes of the Java class library. With myriads of built-in functions, it allows programmers to focus on more important aspects of application rather than reinventing the wheel and spending time and energy in writing string manipulation functions from scratch, therefore the most commonly used string functions come packaged in the Java String class. Some of the useful functions in this class are replace, replaceAll, matches, indexOf, equals, split etc. In addition to these methods, there is another extremely useful method that is used to get a substring, based on some criteria, from a parent string. This method is known as Java String’s substring.

To understand Java String class in detail, take a course at Udemy.com.

What is Java String Substring Method!

The substring method of Java string class, when called on a String type object or a string literal, returns a new string object which is a subset of the original string on which the substring method is called.

The Java string substring method is particularly useful in scenarios where programmers have to get a specific part of a string. For instance, if the email service provider’s name is required, the substring method can be used to obtain the service provider’s name by calling substring method on the index which contains the ‘@’ character. For example, if a user has an Id [email protected], the substring method can be used to get the “company.com” portion of the id. Similarly the substring “company” can also be obtained via the substring method.

The java string substring method has two variants. One method returns a substring starting from specified index to the end of the string. The other method returns substring between the two specified indexes.

How Java String Substring Method Works

The Java substring method takes one parameter. The one parameter is the integer index from which a substring is returned. The returned substring is the character sequence starting from the specified index to the end of the string. The Substring method syntax is as follows:

String substring(int startindex)

The following example demonstrates how actually the Java String substring method works in an application:

String id = new String ("[email protected]");
int index = id.indexOf('@');
String company = id.substring(index +1);

In the above example, the aforementioned scenario where the company name had to be obtained from the email id of the customer is implemented. The String object ‘id’ has been used to store any anonymous id, for instance [email protected] in above case.

Next, an integer variable index is defined that stores the value returned by the indexOf method when it is called on the id string. The indexOf method returns the index of the specified character in the parameter which is ‘@’ in this case.

To get the string after the ‘@’ character, the substring method is used in the next line. An important thing to remember here is that index+1 is passed to the substring method called on the id string. This is due to the fact that company.com starts one index after the index of the ‘@’ character which would return the string “company.com” to the String type object company. The company object has then been displayed on the console.

It is worth mentioning that the String objects are immutable. This means that whenever a string is altered or manipulate by a String class method in such a way that its character sequence is increased, decreased or the sequence of characters in a string is changed, a new string is created and returned to the calling function. The immutability of String type objects might look strange and annoying at first but the memory management benefits brought by it surpass the overhead of creating and new strings.

Therefore, in the above mentioned example, the substring method would not alter the id String object, rather it will create a new string object which contains a substring and this new object would be returned back to the calling function. There is a mutable variant of the String class which is known as StringBuilder where strings are actually altered and altered string objects are returned back to the calling function.

Interested in learning more about Java? Check out a course at Udemy.com.

Java String Substring Overload

In the previous example, a starting index was passed as parameter and the returned substring was the character sequence starting from the starting index till the end of the string. However, there is an overload of the substring method which takes two parameters: Integer type starting index and ending index. The substring returned by this method contains sequence of all characters beginning from the starting index, till the ending index. The syntax of this overload method is as follows:

String substring(int startIndex, int endIndex)

In the following example, the Java String substring method that takes two parameters has been implemented:

String id = new String ("[email protected]");
int startindex = id.indexOf('@');
int endindex = id.indexOf('.');
String company = id.substring(startindex +1, endindex);

In the above example, the scenario has been implemented where only company name is required and .com portion also needs to be omitted from the substring. This functionality can be achieved using the overloaded Java string substring method that takes two parameters. The above example is similar to the last one but in this case another integer variable endindex has been introduced which contains the index of the dot ‘.’ in the id string. The index variable from the last example has been replaced with startindex.

The startindex and endindex variables are then passed to the substring method called on the id. A very important point to note here is that the startindex specifies the starting index of the character sequence, and it includes the character at the start index, which is why 1 is added so that the returned substring starts from the next index. On the other hand, the endindex omits the character at that index, so there is no need to decrement the end index by one. The displayed company string would contain the character sequence “company” all the characters to the right and left to this substring would be omitted.

For more interesting Java tutorials, take a course at Udemy.com.

Page Last Updated: May 2014

Top courses in Java

Java for Beginners
Navin Reddy
4.6 (1,229)
Mastering Java Reactive Programming [ From Scratch ]
Vinoth Selvaraj
4.6 (2,020)
Java Interview Help
Bharath Thippireddy
4.5 (1,458)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.6 (3,964)
Java 8 New Features In Simple Way
4.6 (15,005)
Learn Selenium with Java, Cucumber & Frameworks
Pavan Kumar
4.6 (6,859)
Modern Java - Learn Java 8 Features By coding it
Pragmatic Code School
4.5 (10,699)
Java 21, Java 17, Java 11 and Advanced Java 8
Dr. Seán Kennedy OCA, OCP
4.4 (1,142)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo