• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java String split

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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://coderanch.com/how-to/java/BeForthrightWhenCrossPostingToOtherSites
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Everybody's invited. Even this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic