Question:

Java - splitting a string?

by  |  earlier

0 LIKES UnLike

G'day,

I was just wondering if this is a simple way to split a string where whitespaces appear, I was going to use the split method of the String class however its regex input requires a valid character which ' ' is not one of those, is there any way to achieve this???

Any guidance would be greatly appreciated,

David

ps - just in case my wording was confusing, Im looking to do the following

take a String say "hello david"

and perform an operation so that I return either a list/ array/ etc of the form,

["hello","david"]

 Tags:

   Report

3 ANSWERS


  1. myArray = myString.split("\\s+");


  2. Hey you have tried to give it as str.split(' ') which should be str.split(" ").Hope this will work.

  3. you can make a StringTokenizer:

    String str;

    StringTokenizer st = new StringTokenizer(str," ");

    String[] array = new String[2];

    int index;

    while(st.hasMoreTokens())

    {

        index++;

        array[index] = (String)st.nextToken();

    }

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.