Split a String in Java

Split() : Dividing a string into tokens based on the given delimiters


public class SplitExample
{
    public static void main(String[] args)
    {
        String data = "Core Java,J2EE,Struts,Spring,Hibernate,Javastuff.in";
        String[] strArr = data.split(",");
        for (String str : strArr)
        {
            System.out.println("str = " + str);
        }
    }
}


Output:

str = Core Java
str = J2EE
str = Struts
str = Spring
str = Hibernate
str = Javastuff.in




Using Multiple delimiters:



String str = "This is a sentence.  This is a question, right?  Yes!  It is.";
String delims = "[ .,?!]+";
String[] tokens = str.split(delims);






Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email