< Prev
Next >



split() method



Method split() 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 String values 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 String resulted by splitting a String object with a delimiter value.





Calling split() method on a String object


// 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("Strings formed after splitting : ");
for(String str2: strArr)
{
System.out.println( str2 );
}

}
}


Output is :


Original string is : foo:and:boo
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.

// 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.




Splitting a String object in predefined number of parts.


//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 -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< index() method
replace() method >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2