Advertisement
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. |
// 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 );
}
}
}
Original string is : foo:and:boo
Resulting strings after splitting :
foo
and
boo
// 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 );
}
}
}
Original string is : foo and bar
Strings after splitting :
foo
and
bar
Advertisement
// 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 );
}
}
}
Original string is : This is a String split example
Strings after splitting into 2 parts :
This
is a String split example
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement