| Author |
String parsing algorithm
|
Andy Hahn
Ranch Hand
Joined: Aug 31, 2004
Posts: 225
|
|
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!
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26218
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
|
|
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.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9956
|
|
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).
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Andy Hahn
Ranch Hand
Joined: Aug 31, 2004
Posts: 225
|
|
|
Thanks guys.. I appreciate the help!
|
 |
 |
|
|
subject: String parsing algorithm
|
|
|