Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to split a String into variable length tokens ?

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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

 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know the lengths, just use substring(...)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.)
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the suggestions. To me regex approach seem to be more easy. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic