Briebug Blog

Sharing our thoughts with the community

Using the Java String.split() Method

Using the Java String.split() Method


Today, we're going to talk about the String.split() method for the Java String class. We'll discuss what it does, how to use it, and what to avoid when using it.

Intro to the String.split() Method

The Java String.split() method has two variations, commonly known as method overloading, which are both used to split a String into an array of Strings, using a passed delimiter or regular expression. Let's take a look at the variants.

As you can see, one takes a parameter 'String regex' and the other takes two parameters, 'String regex' and 'int limit'. Our 'String regex' parameter can be a single character, multiple characters, or a custom regular expression to split on multiple matches. First, we'll look at the variation with a single parameter, String regex. This example uses both a single character delimiter and a regular expression to get a feel for the different ways it can be used.

String.split(String regex) Usage

Below is our first example using the first variation and it's output. Be sure to read the code carefully and try it out on your own. When it comes to coding, the best way to get better is practice.


Alright, time to dive into some code!

Output:

So let's explain what is going on here. First, we initialize a String variable, ourString to "This,is,a,comma,separated,string". Next, is where the magic happens. We declare a String array, ourStringSplitArray and set it's value by calling the split() method on ourString. In this example we are using a comma as the delimiter. 


The split() method then returns an array of Strings and stores it in ourStringSplitArray. Last, we loop through the String array and print each value stored in the array. 


Here is a visual to give you a better idea of what is going on. It shows the flow from our original string, to our array of strings.

In our second example, using a regular expression, we are passing "\\d+" for the regex parameter. This is a fairly simple regular expression, that basically says "Split this string every time a number is found". As you can see from output, the string is split when a number is present and the numbers are removed. Depending on your skill level, you can use more complex regular expression for your needs.

String.split(String regex, int limit) Usage

Next up is the second variation of the split() method. This one takes two parameters, String regex and int limit. String regex is used exactly the same way as the first variant, but in this variation, we also pass in the limit.


The limit parameter determines the number of times the delimiter is applied. Because of this, the length of the array returned will also be affected as well. Let's take a look at some examples below to get a better understanding of exactly how the limit works.

Output:

As you can see from the first example, passing 0 for the limit will produce the same result as if you pass no limit at all.  


In the second example, our limit is set to 2. This causes split() create an array with no more than 2 elements. Notice, it pulls the first string before the delimiter and puts it in the first element of the array. Because the limit is 2, the rest of the string is placed into the second element, with the delimiter still in the string.  


Example number 3, with the limit set to 5, does the same as the second example (limit == 2), but the string is split into 5 elements in our String array.


Now that you have seen some examples, try some out on your own. Change the limit, use a different string with another delimiter or regular expression and see how it behaves. Doing this will help you get a better understanding of how split() works and how you can apply it in your own code.

What to watch out for - PatternSyntaxException

PatternSyntaxException is thrown when the regular expression (delimiter) passed to the split() method has an incorrect pattern or has a "tricky" delimiter that isn't escaped properly. Have a look at the example below to see what happens when one of these delimiters or patterns, is passed. 


Note: PatternSyntaxException is a runtime exception. It will not be caught at compile time, so be sure to properly test your code before you deploy it.

Output:

As you can see, when the code runs, it throws the exception when the split() method is called. This is why proper testing is required. You don't want your program to crash and burn in a production environment.  


To fix the above exception, we have options. First, you could simply change the delimiter that you are using. If that is not an option for you, don't worry, there are more available. 


The first option would be to escape the character using backslashes "\\*". See this example and output below.

Output:

As you can see, we are no longer getting the runtime exception and the String is split as expected.


Another option that you have, which is probably the safest, is to wrap your delimiter with Pattern.quote(). Basically what this does, is it handles your delimiter as a literal pattern, instead of giving special meaning to metacharacters or escapes. Let's have a look.


Note: We have to make sure to import java.util.regex.Pattern to use this. You will get a compilation error if the import is missing.

Output

As you can see, it's basically the same output as the example above, where we manually escaped the character. There are way more ways that we can use Pattern.quote() with our regular expression, but that is an entire article itself.

Conclusion

These are the key points to remember about JavaString.split():   


  • It is a convenient method, provided by the Java String class to split a String into an Array of Strings, given the passed delimiter.
  • There are two variants. One containing only the regex parameter and the other containing the regex and "limit", which is used to determine how many times the delimiter is applied.
  • Remember, your regex parameter can be single character, multiple characters, or a custom regular expression. 
  • Beware of PatternSyntaxException. This is a runtime exception that happens when an incorrect pattern or "tricky" delimiter is passed to the split method. 
  • Remember, to avoid or get around the PatternSyntaxException, we can manually escape certain characters or also use Pattern.quote("<our delimiter>").  


I hope you enjoyed this article and were able to gain some knowledge in the process. Remember to play around with the code, experiment with it, and see what you can come up with on your own. Enjoy!

Need Support With a Project?

Start experiencing the peace and security your team needs, and continue getting the recognition you deserve. Connect with us below. 

First Name* Required field!
Last Name* Required field!
Job Title* Required field!
Business Email* Required field!
Phone Required field!
Tell us about your project Required field!
View Details
- +
Sold Out