How to Split String in Java – with Code Sample

0
1156

In this article we will learn how to use Java String’s Split method. Examples will also be shown for quick reference.
The Java String’s split() method splits a String given the delimiter that separates each element and returns an array of String.

Syntax:

[symple_box color=”blue” fade_in=”false” float=”center” text_align=”left” width=””]public String[] split(String regex, int limit)
or
public String[] split(String regex)[/symple_box]

Parameters:

[symple_box color=”blue” fade_in=”false” float=”center” text_align=”left” width=””]

  • regex— the delimiting regular expression.
  • limit— the result threshold which means how many strings to be returned.

[/symple_box]

Return Value:
• It returns the array of strings computed by splitting this string around matches of the given regular expression.

Throws:
PatternSyntaxException – if the regular expression’s syntax is invalid.

Available since JDK: 1.4

Let’s look at the code sample for split method:

public class JavaStringSplit {

	public static void main(String[] args) {
		String str = new String("Welcome-to-goyalsbit.com");
		String temp[];
		String delimiter = "-";
		temp = str.split(delimiter);
		for(int i=0;i<temp.length;i++)
			System.out.println(temp[i]);
		}
}

The returned array will contain 3 elements.
The output of the code above is this:

Welcome
to
goyalsbit.com

Limit
We can also specify the maximum number of items in the returned String Array of the split() method.

Case 1: Limit less than number of items

Let say we have 4 items in the coma separated string and we pass limit value as 2:
String[] vehicles = “Truck,Car,Bike,Scooter”.split(“,”, 2);

The Java String split method will only return 2 items, even when there are 4 possible elements. Here is the equivalent String array:
String[] vehicles = {“Truck”, “Car,Bike,Scooter”};

If we pass the value of 3 for the limit parameter:
String[] vehicles = “Truck,Car,Bike,Scooter”.split(“,”, 3);
Resulting string array would be:
String[] vehicles = {“Truck”, “Car”, “Bike,Scooter”};
Notice again the value of the last element.

Case 2: Limit more than number of items

If we pass a limit value that is more than the number of possible elements, the result is the same as not passing a limit parameter at all. Example :

String[] vehicles = “Truck,Car,Bike,Scooter”.split(“,”, 10);
Is equivalent to

String[] vehicles = {“Truck”, “Car”, “Bike”, “Scooter”};

Regular expression

The delimiter passed to the method is treated as regular expression. For example, the regular expression that represents one or more numeric digits is this:
String delimiter = “\\d+”;

Numeric delimiter
Here is a sample code where we split a String given that the delimiter is a series of numerical digits:
String[] vehicles = “Truck123Car456Bike789”.split(“\\d+”);

This is equivalent to:
String[] vehicles = {“Truck”, “Car”, “Bike”};

Three items are returned because the digits in the String are treated as delimiters.

Alphabet delimiter
Here is the regular expression for a sequence of one or more alphabet letters:
String digitsRegrex = “[a-zA-Z]+”;

An example that uses this regular expression:
String[] items = “123abc456def789”.split(“[a-zA-Z]+”);

The equivalent to:
String[] items = {“123”, “456”, “789”};

Three items we returned because the letters in the String were treated as delimiters.

Previous articleStringJoiner – Java 8 Feature
Next articleCreating Ownership for Change
Pratibha has 7 years of IT industry experience in various domains. She worked as a QA lead for Mobile Applications in R&D of a leading OEM. Playing with different mobile handsets and applications is her passion. Her other interests include Gardening, classical dance and travelling.

NO COMMENTS

LEAVE A REPLY