using java split for string

Hi,

I am trying to use split for space and comma, and this is what i did:

                             
             String str ="Test 0,0,Smile";
               String delims = "\\s+,"
                        if(str.contains(delims))
                        {
                              String []parts = str.split(delims);
                              
                              
                              System.out.println("length " + parts.length);
                        }
                        else
                        {
                              System.out.println("cannot split");
                        }         

I am always getting cannot split .. Am I doing somethng wrong ? Thansk a lot.
zizi21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
String.contains() doesn't understand regular expressions. You might as well call String.split() no matter what. If the string can't be split, you'll get an array with a single element in it.
0
John TsioumprisSoftware & Systems EngineerCommented:
You can use stringTokenizer
0
zizi21Author Commented:
I have now removed the contains and I am always getting length 1 for the example string Test 0,0,Smile

String delims = "\\s+, ";

I thought, I would get
Test
0
0
Smile

Any help to get me started is greatly appreciated.

About StringTokenizer, I read that it is legacy class and is best not used in new codes ..
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

John TsioumprisSoftware & Systems EngineerCommented:
From Java 5 StringTokenizer is marked as legacy but is still going strong and is faster than split
0
CEHJCommented:
I thought, I would get
Test
0
0
Smile

Try
final String DELIMS_PAT = "\\s+|,";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
I think your reg ex is wrong.
Your reg ex tries to match one or more white space characters followed by a comma.
I think what you want is one or more white space characters or a comma

Try this:

String delims = "\\s+|,";

Open in new window

0
zzynxSoftware engineerCommented:
Sorry CEHJ, your comment wasn't there yet when I started typing in mine.
But my comment is not completely redundant since I explain why ;-P
0
krakatoaCommented:
String str = "Test 0,0,Smile".replace(" ",",");
  String[] strSpl =str.split(",");

Open in new window

0
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.