LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW

1. Overview

In this tutorial, we'll learn how to split a Java String only on the first occurrence of a delimiter using two approaches.

2. Problem Statement

Let's say we have a text file having each line as a string made up of two parts – the left part indicating a person's name and the right part indicating their greeting:

Roberto "I wish you a bug-free day!"
Daniele "Have a great day!"
Jonas "Good bye!"

Subsequently, we want to get the person's name from each line.

We can see that both the parts are separated by a ” ” (space), just like the other words in the right part. So, our delimiter is going to be the space character.

3. Using the split() Method

The split() instance method from the String class splits the string based on the provided regular expression. Moreover, we can use one of its overloaded variants to get the required first occurrence.

We can provide a limit as a parameter to the split() method to specify the number of times we want to apply the pattern and thus the maximum number of tokens in the resulting array. For example, if we put the limit as n (n >0), it means that the pattern will be applied at most n-1 times.

Here, we'll be using space (” “) as a regular expression to split the String on the first occurrence of space.

As a result, we can tokenize each line into two parts using the overloaded split() method:

public String getFirstWordUsingSplit(String input) {
    String[] tokens = input.split(" ", 2);
    return tokens[0];
}

So, if we pass the first line from our example as an input to this method, it will return “Roberto”.

However, if the input String has only one word or has no space in it, the above method will simply return the same String.

Let's test this out:

assertEquals("Roberto", getFirstWordUsingSplit("Roberto \"I wish you a bug-free day\""));
assertEquals("StringWithNoSpace", getFirstWordUsingSplit("StringWithNoSpace"));

4. Using the substring() Method

The substring() method of the String class returns the substring of a String. It's an overloaded method, where one of the overloaded versions accepts the index and returns all the characters in the string until the given index.

Let's combine substring() and indexOf() to solve the same problem.

Firstly, we'll get the index of the first space character. Then, we'll get the substring until this index which will be our result, the person's name:

public String getFirstWordUsingSubString(String input) {
    return input.substring(0, input.indexOf(" "));
}

If we pass the same input String as before, our method will return the String “Roberto”.

However, if the input String doesn't contain any spaces, then this method will throw StringIndexOutOfBoundsException. If a match is not found, the indexOf() method returns -1.

To avoid this exception, we can modify the above method:

public String getFirstWordUsingSubString(String input) {
    int index = input.contains(" ") ? input.indexOf(" ") : 0;
    return input.substring(0, index);
}

Now, if we pass a String with no space to this method, we will get an empty String in return.

Let's test this out:

assertEquals("Roberto", getFirstWordUsingSubString("Roberto \"I wish you a bug-free day\""));
assertEquals("", getFirstWordUsingSubString("StringWithNoSpace"));

5. Conclusion

In this article, we have seen two approaches to split a String only on the first occurrence of a delimiter in Java.

As always, the code is available over on GitHub.

LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments