Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting a memo field contents into strings 2

Status
Not open for further replies.

Bryan - Gendev

Programmer
Jan 9, 2011
408
AU
I have a user who wants to insert text into one of my fields via a text file.

I have created a system whereby I give the user 3 254 chr strings to compose the contents.

I then concatenate them and add into the Memo field in my code.

I am writing out the original Memo field into 3 strings into a text file as a starting point

I am wondering whether anyone can suggest a better way than

myCaption_1 = Subst(TEMP_C.Caption,1,254)
myCaption_2 = Subst(TEMP_C.Caption,255,254)
myCaption_3 = Subst(TEMP_C.Caption,255+254+1,254)

where a space is chosen as the break point rather than splitting up a word at the end of the string.

It is unlikely that more than 3 X 254 chrs will exist in the Memo field.

Thanks for any thoughts

GenDev
 
[ ]

One way would be to do something like this:

[tt]TempStr = TEMP_C.Caption

z = 0

DO WHILE NOT EMPTY(TempStr)

z = z + 1
DIMENSION aArray[z]

JunkStr = LEFT(TempStr, 254)
aArray[z] = LEFT(JunkStr, RAT(" ", JunkStr) - 1)
TempStr = STUFF(TempStr, 1, LEN(aArray[z]) + 1, "")​

ENDDO


===> Assign your variables here from the resulting array

RELEASE aArray[/tt]

This assumes that there is at least one space every 254 characters. Also assumes that there are no consecutive spaces.

Not tested.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
GenDev,

Is there any special reason for giving the user 3 x 254-char fields? A more obvious approach would be to let them edit the whole thing in an edit box. That way, you would have no restriction on the length of the text, and you'd be able to save the whole thing to a memo field in one step.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Mike,

I neglected to say that the table is output as an excel worksheet - to allow easier editing of the fields - there are a number of other fields produced in the excel file.

GenDev
 
Yes, I'm, with Mike: Why not let it be a memo field and use an editbox for editing?

By the way the three starting points of the substrings would be 0*254+1, 1*254+1 and 2*254+1, you got the last one wrong with 255+254+1, because the second string goes from position 255 to position 255+253, and you let the third string begin at position 255+254+1, you skip 1 byte.

If you still want to let users edit lines use ALINES or MEMLINES.

Bye, Olaf.
 
So, you're saying that you need three sub-strings because these will end up as three cells in the worksheet? Well, I can see how that might make it easier to edit within Excel.

To go back to the original question, have you considered using GETWORDCOUNT() and GETWORDNUM()? That would let you loop through the combined string, one word at a time. Add each word in turn to the output string until you exceed the limit, then move on to the next output string. That approach would have the advantage that it would deal correctly with punctuation as well as spaces.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Agree with the solutions provided. But I do not see why 254 Chars vs 3*254 would be better for Excel. However, 120 Char(or so) could be visible on the whole screen, depending on the font/resoulution. Option: exporting full string and set 'wrap text' in the Excel Column.

 
Thanks everyone for your insights.

I have mmerlins code working so will stick with that - many thnks.

In my user's case he has 12000+ records which he may want to add text to in the 4 subject fields so excel is his preferred methodology. My app already has an editable memo field in a grid but he wants to import en mass via the csv generated txt tab delimited option I have in my app already.

GenDev
 
GenDEv,

mmerlins code can also be done with ALINES, as it can also break up at spaces, not only at line feeds:

ALINES(aArray,table.memofield," ")

This replaces mmerlins code. The words are then in aArray elements, as with his code. You can specify as many seperator chars as you want, as the third parameter of ALINES.

Bye, Olaf.

 
Olaf,

I replaced mmerlins code( working as I wanted) with your expression.

But of course it broke the memo contents into individual words.

GenDev
 
OK, I tried out mmerlins code and there is a difference, it breaks at the last space within 254 chars via the RAT() inside the LEFT. But you can also put together words until you get at over threshold length.

Besides you can turn word wrap on in Excel cells, that way you don't need to change your memo value in any way.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top