• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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: 40791
828
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: 13013
66
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!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic