How to execute a string split function in Java

Given a string, we can break it into one or more parts on the basis of a condition. This conditional breaking of a string is considered splitting the string.

In the case of the Java implementation of the function, the split string is returned as a character array.

How do you implement a string split?

The string split function in Java can be implemented in two ways. Let’s look at both:

1. Split(regex, limit)**

In this implementation, the split method takes in two arguments:

  • Regex is the regular expression which will be used as the condition of breaking the given string, it is usually a string.

  • Limit, an integer, is the number of pieces we want to create from the string. This in itself can be categorized in three ways:

    • limit == 0: This signifies that break the given string on every occurrence of the condition, and remove all whitespace at the end of the string

    • limit<0: This signifies the same as above except it does not remove the trailing whitespaces

    • limit > 0: This signifies that the number of elements in the character array should be equal to the limit hence, the condition is searched for only limit - 1 times

    To understand this better, let’s look at the examples below

class HelloWorld {
public static void main( String args[] ) {
String greeting = "a,b,c,d,,";
String [] split_greeting;
System.out.println( "Limit is equal to 0" );
split_greeting = greeting.split(",",0);
for(String piece : split_greeting){
System.out.println(piece);}
System.out.println( "Limit is equal to -1" );
split_greeting = greeting.split(",",-1);
for(String piece : split_greeting){
System.out.println(piece);}
System.out.println( "Limit is equal to 3" );
split_greeting = greeting.split(",",3);
for(String piece : split_greeting){
System.out.println(piece);}
System.out.println( "Limit is equal to 6" );
split_greeting = greeting.split(",",6);
for(String piece : split_greeting){
System.out.println(piece);}
}
}
Looking at the code above through illustration

2. Split(regex)**

The second implementation simply takes in a regular expression and splits the string on the basis of that condition. In this implementation, it takes in no value for limit, which is then assumed to be 0.

The code below shows the exact same example as above, but with no limit value. See how the result matches with that of when 0 was used as the limit.

class HelloWorld {
public static void main( String args[] ) {
String greeting = "a,b,c,d,,";
String [] split_greeting;
System.out.println( "The limit is not entered" );
split_greeting = greeting.split(",");
for(String piece : split_greeting){
System.out.println(piece);}
}
}

Example

We can even separate a string on the basis of a letter or spaces between words, the examples below show how to do this.

class HelloWorld {
public static void main( String args[] ) {
String greeting = "Hello World! Hi!";
String [] split_greeting;
System.out.println( "Separating on the letter H" );
split_greeting = greeting.split("H",0);
for(String piece : split_greeting){
System.out.println(piece);}
System.out.println( "Separating on the spaces" );
split_greeting = greeting.split("\\s",0);
for(String piece : split_greeting){
System.out.println(piece);}
}
}

What happens if the given expression is not present in the string? The code will simply return an empty character array.

Look at the example below to see this!

class HelloWorld {
public static void main( String args[] ) {
String greeting = "Hello World! Hi!";
String [] split_greeting;
System.out.println( "Separating on a regex that does not exist in the string" );
split_greeting = greeting.split(".",0);
for(String piece : split_greeting){
System.out.println(piece);}
}
}
Copyright ©2024 Educative, Inc. All rights reserved