• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java String split

 
Dorothy Taylor
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Maneesh Godbole
Saloon Keeper
Posts: 11097
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic