How to split a string in Java

We can split the string in jave with below ways

Split method

static private void bySplit(String s)
{
    String[]ss=s.split(" ");
    for(int k=0;k<ss.length;k++)
        System.out.println(ss[k]);
}

 

By StringTokenizer class

static private void byTokenizer(String s)
{
    StringTokenizer st=new StringTokenizer(s," ");
    while(st.hasMoreElements())
    {
        System.out.println(st.nextToken());
    }
}

 

static private void byTokenizer(String s)
{
    StringTokenizer st=new StringTokenizer(s," ");
    while(st.hasMoreElements())
    {
        System.out.println(st.nextToken());
    }
}

static private void bySplit(String s)
{
    String[]ss=s.split(" ");
    for(int k=0;k<ss.length;k++)
        System.out.println(ss[k]);
}

public static void main (String[] args)
{
    byTokenizer("Welcome to Java");
    bySplit("Welcome to Java");
}

 


Flutter Questions
Android Questions