Home >>Java String Methods >Java String split() method

Java String split() method

Java String split() method

Java split() method in java string is used to splits this string against provided regular expression and returns a char array to it.

Syntax

public String split(String regex)  

public String split(String regex, int limit) 

Parameters

regex - This parameter is used to applied regular expression on string.

limit - This parameter is used to limit for the number of strings in array.

Returns

It is used to return an array of strings

Throws

PatternSyntaxException if pattern for regular expression is invalid

Java String split() Method Example 1


public class SplitExample1
{  
public static void main(String args[])
{  
String n1="Welcome to phptpoint";  
String[] words=n1.split("\\s");
for(String w:words)
{  
System.out.println(w);  
}  
}  
}

Output:
Welcome
to
phptpoint

Java String split() Method Example 2


public class SplitExample1 
{  
    public static void main(String[] args) 
	{  
        String str1 = "Phptpointtt";  
        String[] arr1 = str1.split("t", 0);  
        for (String w : arr1) 
		{  
            System.out.println(w);  
        }  
        System.out.println("Split array length: "+arr1.length);  
    }  
} 

Output:
Php
poin
Split array length: 2

No Sidebar ads