Advertisement



< Prev
Next >



Java String split() Method



The split() method of the String class is used to split the String object in parts using a delimiter value, and then it returns an array containing these split parts. These parts are individually a String object.




Some versions of split() method


Methods Description
public String[] split(String delimiterValue)

It returns a String array, which contains some strings resulted by splitting a String object with a delimiter value.


public String[] split(String delimiterValue, int max)

It returns a String array, which contains the max number of strings resulted by splitting a String object with a delimiter value.







Calling split() method on a String object


// Java - Calling the split() method of String, with a delimiter value :


class StringSplit
{
public static void main(String[] ar)
{
String str1= new String("foo:and:boo");
System.out.println("Original string is : "+ str1);


//Splitting the value in a String object with a delimiter value :
String strArr[]=str1.split(":");


//Extracting and printing String values in a String array after splitting
System.out.println("Resulting strings after splitting : ");
for(String str2: strArr)
{
	System.out.println( str2 );
}

}
}


Output is :


Original string is : foo:and:boo
Resulting strings after splitting :
foo
and
boo


Program Analysis


In the last code, we have split the value in a String object into parts using a delimiter value :




Splitting a String object using a delimiter value, space.

// Java - Calling the split() method of String, with a delimiter value of space \s


class StringSplit2
{
public static void main(String[] ar)
{
String str1= new String("foo and bar");
System.out.println("Original string is : "+ str1);


//Splitting the value in a String object with a delimiter value of space  \s
String strArr[]=str1.split("\\s"); //Depending on the Operating System, an extra \ may be required.


//Extracting and printing String values in a String array after splitting
System.out.println("Strings after splitting : ");
for(String str2: strArr)
{
	System.out.println( str2 );
}

}
}


Output is :


Original string is : foo and bar
Strings after splitting :
foo
and
bar


Program Analysis


In the last code, we have split the value in a String object into parts using delimiter value of space \s. An extra \ may be required on some specific Operating System, hence we have used \\s.


Advertisement




Splitting a String object in predefined number of parts.


// Java - Calling the split() method of String, with predefined number of parts


class StringSplit3
{
public static void main(String[] ar)
{
String str1= new String("This is a String split example");
System.out.println("Original string is : "+ str1);


//Splitting the value in a String object with a different delimiter value of space 
String strArr[]=str1.split(" ", 2); // We are asking for 2 partitions of String.


//Extracting and printing String values in a String array after splitting
System.out.println("Strings after splitting into 2 parts : ");
for(String str2: strArr)
{
	System.out.println( str2 );
}

}
}


Output is :


Original string is : This is a String split example
Strings after splitting into 2 parts :
This
is a String split example


Program Analysis


In our last code, we have split the value in a String object into 2 parts by using delimiter value of a space, i.e. " ".



Please share this article -





< Prev
Next >
< index() method
replace() method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement