• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to split a String into variable length tokens ?

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Is there any built in library or api provided by Sun or other open source projects, where it would let me split a String into variable length tokens ?

For eg, If I have a string like below



If I want to split this string into tokens, based on variable length tokens, by providing size in some sort of regex (I could not put this in write wordings)

To be more specific, I should be able to split this string based on a copybook structure defined in COBOL. like

01 STRING1 PIC X(3)
02 STRING2 PIC X(5)
etc

I can write my userdefined function, but wondering if anything already existing.
Thanks

 
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
If you know the lengths, just use substring(...)
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to find a particular token to split on, you can use String#split(java.lang.String).
Or try one of the overloadings of String#indexOf() to find where to start your substrings.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a regex will also let you specify how many characters to match:

[a-z]\{4,8\}

will match 4,5,6,7, or 8 lowercase letters.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:a regex will also let you specify how many characters to match:

[a-z]\{4,8\}

will match 4,5,6,7, or 8 lowercase letters.


Actually, it'd be:

(No backslash before the braces. I always get confused about what to backslash. It's different among Java, sed, egrep, and vi.)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Raja wrote:I can write my userdefined function, but wondering if anything already existing.


Seems to me you have 2 tasks:
1. Splitting your string based on predefined lengths.
2. Getting those predefined lengths from a piece of COBOL source.
And of the two, number 2 is going to be a lot harder (the first, as Darryl said, can be done with simple substring()s).

Winston
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:
Actually, it'd be:

(No backslash before the braces. I always get confused about what to backslash. It's different among Java, sed, egrep, and vi.)


Thanks...yeah, I do a lot of trial and error with that myself...
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the suggestions. To me regex approach seem to be more easy. Thanks.
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic