• 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

String parsing algorithm

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a database table that contains a comments column with a 4000 char limit. I want to write an algorithm that can take a variable length comment String (for example 10,000 chars) and break it up into multiple comment records. The database part is easy, its the dynamic parsing of the comment String into multiple Strings. Did I mention I also need to make sure that each comment does not end in the middle of a word, so that means space separated. If anyone has any pseudo code or ideas or knows of any libraries or utils please let me know!

Thanks!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andy,
Could you use a regular expression to do this? I'm thinking a regular expression that is up to 4,000 characters and ends with either a whitespace character or the end of the string.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a lot of ways this could be done. The pros seem to prefer regex, personally I find regex can be tricky to implement, but that's just me. If you have a lot of parsing to do regex is probably most efficient.

 
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
simple brute force might work, too.

Go to position 4000. is it a space? then go to 3999. is it a space? etc...

when you find a space as position X, get substring from 0 to X.

Then go to X + 4000, and repeat.

(note: I may not have my fenceposts exactly right, but you get the idea, I hope).
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.. I appreciate the help!
reply
    Bookmark Topic Watch Topic
  • New Topic