Understanding the Java Split String Method
The Java String class is one of the most widely and extensively used classes in the Java class library. The String class comes with variety of built-in functions that can be leveraged in your code. These built-in functionalities save developers from dwelling on nitty-gritty details of string manipulation. Some of the most commonly used String class methods include replace, matches, equals, indexOf, and replaceAll. One such String class methods is the Java split string method that can be used to split a string into two or more strings based on some criteria. This article focuses on this Java split string method.
Want to learn more about Java? Take a course at Udemy.com.
What is Java “Split” String Method?
Often, during Java application development, a need arises where a string entered by the user or any other source has to be chopped into small pieces based on some criteria. For instance, consider a List control where user is asked to enter several elements separated by a comma or a white space, which he wishes to appear in the list. To get all the elements that the user entered and to present them in the form of a list, a method is required that can be used to chop or split a long string into manageable individual elements.
How Java “Split” String Method works?
The “split” method can be called on a String type object or a string literal. It takes a String literal or a String object as a parameter, which is the delimiter. In the string on which split method is called, whenever the delimiter value is encountered, the string is split at that point. At the end, all the chopped strings are added into a String type array and that array is returned to the calling function.
Java “Split” String Method, A basic Example
To see how the “split” method actually works in an application, take a look at the following line of code:
String str= "Udemy is the best site for video tutorials";
String criteria= " ";
String [] strarr = str.split(criteria);
for( int i =0; i<strarr.length; i++)
{
System.out.println(strarr[i]);
}
In the above sample code, a String object str has been instantiated with a literal string “Udemy is the best site for video tutorials”. Another criteria is instantiated with another string literal space (“ ”). This space actually acts as a delimiter. Next, a String type array strarr is instantiated, which receives the array returned by the “split” method.
The “split” method is called on the str object and criteria string is passed to it as a parameter. It means that whenever the “split” method encounters a space character (“ ”), it should chop the string. Finally, after str string is chopped into small substrings, these strings would be returned back to the calling function the form of array of string objects. In the aforementioned example, the elements of the returned array have been displayed using a “for” loop. The output of example would contain all the words in the str string, displayed one per line as shown below:
Udemy
is
the
best
site
for
video
tutorials
Apart from the String objects, “split” method can also be called on string literals. For example, the following method works similar to the one described in the last example:
String [] strarr = "Udemy is the best site for video tutorials".split(" ");
Here, “split” method has been called on a string literal and the parameter passed to it is also a string literal i.e. space (“ ”).
To learn more about String class in Java, take a course at Udemy.com.
Java String Split overload
The split String method in Java has an overload apart from the one that takes only one parameter of type string.
Split (String criteria, int limit)
The overload is the method that takes two parameters. The first parameter is the string that serves as the delimiter as in the last example and the second parameter is the number of split strings stored in the returned array of strings. This concept is best explained with the help of following example.
String str= "Udemy is the best site for video tutorials";
String criteria= " ";
String [] strarr = str.split(criteria,3);
for( int i =0; i<strarr.length; i++)
{ System.out.println(strarr[i]);
}
The above example is similar to the one presented earlier in the article. However, in this case the Java String “split” method takes two parameters. The first is the criteria (“ ”) as it was in the previous. The second parameter is the limit that specifies the number of strings returned, which are three in this case. The output of the above code is as follows:
Udemy
is
the best site for video tutorials
It is evident from the output that the “split” method has chopped the string to three substring based on the criteria. When the number of strings returned reaches the limit, no more chopping is done and the chopped strings are returned.
Similar to the split method takes one parameter, the split (String criteria, int limit) can also be applied to the String literals other than the string type objects. For instance, the last example can be altered as follows to explain this concept:
String [] strarr = "Udemy is the best site for video tutorials".split(" ",3);
for( int i =0; i<strarr.length; i++)
{
-
out.println(strarr[i]);
}
In the above code, the split method is called on a literal string and the criteria parameter taken is also a string literal. The output of the above will be similar to the one in the last example.
The Java Split string method is extremely useful when used intelligently in string handling scenario. Java String class contains variety of such methods that can significantly reduce developers’ task by providing solution right out of the box.
To learn more about Java language and the string handling functions, take a Udemy course!
Recommended Articles
Top courses in Java
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.