Solved

How to split a string in java with different separators?

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 52
stringclean challenge 26 59
Java Message handling in Service Layer 3 39
maven project error 5 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

15 Experts available now in Live!

Get 1:1 Help Now