Java String split() method with examples
In this tutorial, we are going to learn about the Java String split() method with examples. It is one of the predefined methods in the Java String
class. The basic purpose of this method is to break the given string around a given regular expression. There are two versions of split() method in Java. One that takes two parameters and another one that takes only one parameter. This method split() returns an array of strings. So, I have divided this tutorial into two sections. So, let’s begin with the one with only one parameter.
public String [ ]split(String regexp)
This version takes only a single parameter around which the string has to be broken. For example, look at the code below-
class data { public static void main(String ar[]) { String p="[email protected]@com"; String a[]=p.split("@"); for(int i=0;i<a.length;i++) { System.out.println(a[i]); } } }
Output-
CodeSpeedy email com
As we can see the given string [email protected]@com
has been split around @
. Hence, we can use the same with other regular expressions like ., ” “, %, etc.
Here we see that the string gets broken down into three parts because there are two @
. But what if we want to break the string into two parts only i.e., considering the only first occurrence of @
. So, how do we do that? So, for this, we have another version of split() method that accepts two parameters. Let’s learn how to use it.
Also read: How to split a string by space in Java
public String[ ] split(String regexp, int l)
This has an additional parameter of integer type that tells about the limit in which the string needs to be broken. For example, taken above, let’s we want to break the given string into two parts only considering the only first occurrence of @. So, to do this we set the limit l=2. So, let’s see the code-
class data { public static void main(String ar[]) { String p="[email protected]@com"; String a[]=p.split("@",2); for(int i=0;i<a.length;i++) { System.out.println(a[i]); } } }
Output-
CodeSpeedy [email protected]
Here, the given string is divided into two parts neglecting the second occurrence of @.
In this case, the value of l
can be greater than 0, or maybe less than 0, or maybe equal to 0. When l=0
, then it will be the case of the upper version of the split() method i.e., that accepts only a single parameter.
Let’s see some examples now so that it will become more clear,
class data { public static void main(String ar[]) { String p="[email protected]@[email protected]@[email protected]"; String a[]=p.split("@",5); for(int i=0;i<a.length;i++) { System.out.println(a[i]); } } }
Output-
CodeSpeedy email com Internet [email protected]
As you can see in this example, the value of l is equal to 5. So, the maximum length of the array a
cannot be greater than 5. Also, pattern matching can be applied l-1 times only. Let’s see another example with the value of l
=-1
.
class data { public static void main(String ar[]) { String p="[email protected]@[email protected]@[email protected]"; String a[]=p.split("@",-1); for(int i=0;i<a.length;i++) { System.out.println(a[i]); } } }
Output-
CodeSpeedy email com Internet vintcerf timberners
Here we can see l
is set to be equal to -1. So, in this case, pattern matching is applied until possible and there is no limitation on array a
length.
So, that’s the end I hope that this tutorial helps you.
Leave a Reply