File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java String split Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java String split" Watch "Java String split" New topic
Author

Java String split

Dorothy Taylor
Ranch Hand

Joined: Nov 26, 2007
Posts: 104
Hi

I have a string like this:



I want to split first on ; and then on :
Finally the output should be only the latter part around : i.e. my output should be def, ghi, jkl, pqr, stu, yza,aaa,bbb

This can be done using Split twice i.e. once with ; and then with : and then pattern match to find just the right part next to the :. Howvever, is there a better and optimized solution to achieve this?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Did you try it and see if it can work? Don't be afraid to try new things, even if you are not sure they will do the job. That's how you can learn a lot.

So, write the code for the first task (split around ';') and then see what you get. Then try the second to split around ':' and see what you will get there. Perhaps you won't need any regular expressions whatsoever.


The quieter you are, the more you are able to hear.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Dorothy Taylor wrote:my output should be def, ghi, jkl, pqr, stu, yza,aaa,bbb


You need to be more precise. Is that output (a comma-separated list of) String[] array elements, or a single String? Is the presence or absence of spaces between each set of three characters and a comma significant in a way you are yet to explain, or a quirk of inconsistent typing?


luck, db
There are no new questions, but there may be new answers.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

Dorothy Taylor wrote:Hi

I have a string like this:



I want to split first on ; and then on :
Finally the output should be only the latter part around : i.e. my output should be def, ghi, jkl, pqr, stu, yza,aaa,bbb

This can be done using Split twice i.e. once with ; and then with : and then pattern match to find just the right part next to the :. Howvever, is there a better and optimized solution to achieve this?



Crossposted at http://codereview.stackexchange.com/questions/18485/split-string-for-returning-only-the-latter-part
Please read https://www.coderanch.com/how-to/java/BeForthrightWhenCrossPostingToOtherSites


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18971
    
  40

Dorothy Taylor wrote:Hi

I have a string like this:



I want to split first on ; and then on :
Finally the output should be only the latter part around : i.e. my output should be def, ghi, jkl, pqr, stu, yza,aaa,bbb

This can be done using Split twice i.e. once with ; and then with : and then pattern match to find just the right part next to the :. Howvever, is there a better and optimized solution to achieve this?


It actually can be done with a single split() method call -- but "better" would be relative. It may be "optimized" but it will also be likely difficult to read.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java String split