Solved

How to split a string in java with different separators?

Posted on 2013-11-12
3
278 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

776 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