Solved

How to split a string in java with different separators?

Posted on 2013-11-12
3
284 Views
Last Modified: 2013-11-12
Hi,
I have the following input strings and I would like to split these strings in JAVA:

Input:
666666,k22      333

Open in new window


Input:
k22 666666,  333

Open in new window


or

Input:
k22, 333  666666

Open in new window


or

Input:
k22,333,666666

Open in new window


The expected out from all these input is:

Output:
k22
333
666666

Open in new window


The general rules are:
1) The number can start with or without k.
2) The number of digits may vary
3) There can be one or more spaces or a comma or both between the numbers


How can I write this split in Java?
0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39642988
So your delimiters are space and comma. You could combine these into a regular expression and split on either using the String class' Split method.

e.g.

String original = "666666,k22      333";
String[] result = original.split("[ ,]+");

Open in new window


...where [ ,]+ one or more ( + ) of either a space or a comma ( [ ,] ).
0
 

Author Comment

by:Tolgar
ID: 39643024
Sorry I made a mistake for the output:

It should be like this:

Output:
22
333
666666

Open in new window


The general rule is:
- There shouldn't be any "k" before the number in the output
0
 

Author Comment

by:Tolgar
ID: 39643132
Well, I replaced all "k" characters before the split. It works now.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question