Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Split() String Method in Java

Last updated by Vartika Rai on Apr 01, 2024 at 01:48 PM | Reading time: 10 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Split() String Method in Java
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

In coding interviews, software engineers are often given a problem statement that requires parsing complex inputs. This is where Java coders can stand out, as the String.split method helps in parsing complex strings easily using regex delimiters. This article discusses the different use-cases of the split method in detail.

List of topics covered in this article:

  • What is split() string in Java?
  • What are delimiters?
  • How should delimiters be treated?
  • How are special characters treated as delimiters in the split method?
  • Split string based on multiple characters used as delimiter
  • Syntax/Parameter
  • String split() Method throws PatternSyntaxException
  • Advantages and Disadvantages of Java String split() method

What Is split() String in Java?

When we have multiple pieces of information in a string, we need to parse the string to extract the data as individual pieces. For example, parsing data from a file line by line.

The split method splits a string into multiple strings based on the delimiter provided and returns the array of strings computed by splitting this string around matches of the given regular expression.

The array returned by the method contains each substring terminated by another substring that matches the given expression or terminated by end of string. The order of substrings returned will be the same as they occur in the given string. If there is no match found for the delimiter provided, the resulting array of string will contain the single string provided.

Strings in Java can be parsed using the split method. 

Following are the points to be considered while using the split method:

  1. What are delimiters?
  2. How should delimiters be treated? 

What Are Delimiters?

In Java, delimiters are characters that separate the strings into tokens. We can define any character as a delimiter in Java. 

How Should Delimiters Be Treated?

The delimiters should be treated as a separating character. This requires a little bit of knowledge of regular expressions as well. Let’s take an example.

Example 1: 

We want to divide the string based on space character. 

        String text = "This is simple text";

        String [] result = text.split(" ");

        for(String word: result) {

        System.out.println(word);

        }

Output:

This

is

simple

text

Example 2: 

Now, let’s change our previous example a little bit.

String text = “This       is     simple   text”

We want to split this text as we have done in our previous example. We want to extract the words from the given text using the split method. But here, if we apply text.split(“ ”), we won’t get the expected result because the text contains multiple consecutive spaces, and a split of consecutive spaces will result in empty strings. Now, the question is — how should we treat consecutive delimiters?

For this example, we want consecutive spaces to be treated as one:

        String text = "This       is     simple   text";

        String [] result = text.split("[ ]+");

        for(String word: result) {

              System.out.println(word);

        }

In the above example, we have used [ ]+ as a delimiter. The delimiter is written inside square brackets. This form is a kind of regular expression. The plus sign is used to indicate that consecutive delimiters should be treated as one.

How Are Special Characters Treated as Delimiters in the split() Method?

Say we want to split the string based on “|” or “.” The most obvious solution would be to split by a special character. But this will not yield the result we want. 

        String text = "This|is|simple|text";

        String [] result = text.split("|");

        for(String word: result) {

        System.out.println(word);

        }

This program will print the following output:

To solve this problem, we have to include escape characters along with special characters as delimiters to get the expected result. 

        String text = "This|is|simple|text";

        String [] result = text.split("\\|");

        for(String word: result) {

              System.out.println(word);

        }

Is there any other way to solve this problem? Yes, we can solve this problem using a special character inside a bracket. Let’s see the sample code for better understanding:

        String text = "This|is|simple|text";

        String [] result = text.split("[|]");

        for(String word: result) {

              System.out.println(word);

        }

Split String Based on Multiple Characters Used as Delimiter

Suppose we have a string containing several sentences that use only commas, periods, question marks, and small case English letters. We want to split the string based on commas, periods, and question marks. 

        String text = "This,,,is?not.simple.text";

        String [] result = text.split("[,?.]+");

        for(String word: result) {

        System.out.println(word);

        }

Output: 

This

is

not

simple

text

So, we can split the string based on multiple characters used as delimiter. We have to put all the splitting characters inside the bracket([]). Here, we have used [ ]+ as a delimiter. The plus sign (+) is used to indicate that consecutive delimiters should be treated as one.

Using regex With split 

So far, we've understood what split() does and also covered the various ways in which delimiters can be treated.  Now, let's see how we can use regex along with the split method.

Following is the syntax for using regex along with split:

String[] split(String regex)  : This will work in a similar manner to split(String regex, limit = 0). 

String[] split(String regex, int limit)

Regex: This is a delimiting regular expression. 

Limit:  The limit parameter determines how many times the pattern is applied, and therefore it affects the length of the resultant array.

  • If limit > 0, the pattern will be used to cut the string (limit-1) times.
  • If limit < 0, the pattern will be used to cut the string as many times as possible.
  • If limit = 0, the pattern will be used to cut the string as many times as possible, but it will be discarded if there is an empty string behind.

Let’s see some examples with limit parameters. 

Example 1: 

If we set the limit as 0, the pattern will be applied as many times as possible. The resulting array will return all strings that are separated by delimiters provided. Have a look at the example:

        String text = "This is a simple text";

        String [] result = text.split(" ",0);

        for(String word: result) {

        System.out.println(word);

        }

Output: 

This

is

a

simple

Text

Example 2: 

If we provide a positive limit, then the pattern will be applied to (positive_limit -1). 

        String text = "This is a simple text";

        String [] result = text.split(" ", 2);

        for(String word: result) {

        System.out.println(word);

        }

Output: 

This

is a simple text

Possible Exceptions That String.split() Method Can Throw

While using the String.split() method in Java there are some common exceptions that we encounter. Mainly: PatternSyntaxException and NullPointerException. Lets discuss more about when these exceptions occur.

PatternSyntaxException

If the delimiter regular expression is not a valid syntax, it will throw a PatternSyntaxException. Let’s check the example below. 

        String text = "This is a sim\\ple text";

        String [] result = text.split("\\");

        for(String word: result) {

        System.out.println(word);

        }

The problem is backslash is an escape character for other special characters like “.” or “|”

If we want to split the string from \\, we have to introduce escape characters for this too. In Java, each of these backslashes needs to be escaped again. Have a look at the following program:

        String text = "This is a sim\\ple text";

        String [] result = text.split("\\\\");

        for(String word: result) {

        System.out.println(word);

        }

Output: 

This is a sim

ple text

NullPointerException

The split method does not accept a null argument. It will throw java.lang.NullPointerException.

          String text = "This is a simple text";

        String [] result = text.split(null);

        for(String word: result) {

        System.out.println(word);

        }

Advantages and Disadvantages of Java String.split() Method

Advantages:

  • In most coding competitions, the split method comes in handy to split the given input and parse it accordingly.
  • Using regex inside the split function, we can parse any string and extract the desired text.
  • Flexible to use.

Disadvantage:

  • String.split() is slow in performance when compared with StringTokenizer in Java.

Examples of Interview Questions on Java String split() Method

  1. Given a string, check whether it's a valid email-id or not using regex? Email should consist of ‘@’ and a valid domain of length >= 4.
  2. Given a string phone number that is separated by hyphens, check if the phone number is valid or not?
  3. You are given a string that consists of words that are duplicated. Write a program to remove all the duplicated words.

FAQs on Java String split() Method

Question 1: Between String.Split and StringTokenizer, which is better?

Answer: Generally, StringTokenizer is faster in terms of performance, but String.split is more reliable. The split method of String and the java.util.regex package incur the significant overhead of using regexes, hence making it slow. StringTokenizer does not use java.util.regex and therefore gives better performance. On the other hand, String split returns an array of results and is more convenient to use than StringTokenizer.

Question 2: What is the best application of the string split method?

Answer: For validating official email IDs, we can easily split the string using the @ symbol, and then we can validate both the email as well as the domain name in the resultant array. It can also be used to parse data from a file line by line.

Preparing for a Tech Interview? 

If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview preparation, we have trained thousands of engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

---------

Article contributed by Problem Setters Official


Last updated on: 
April 1, 2024
Author

Vartika Rai

Product Manager at Interview Kickstart | Ex-Microsoft | IIIT Hyderabad | ML/Data Science Enthusiast. Working with industry experts to help working professionals successfully prepare and ace interviews at FAANG+ and top tech companies

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

Split() String Method in Java

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image