Solved

How to split a string in java with different separators?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 50
what is a "java.lang.System Property"   ? 20 57
split string containing \r\n in Java 46 38
tomcat administrtor 12 32
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

867 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now