Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to split a string in java with different separators?

Posted on 2013-11-12
3
Medium Priority
?
289 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

885 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