Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Memo field parsing routine that doesnt break words
Message
From
26/03/2013 21:07:59
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Miscellaneous
Thread ID:
01569314
Message ID:
01569315
Views:
68
>anyone have an easy-to-use routine that returns say the first 150, 200, 250 (etc) chars
>from a memo field but avoids breaking words in the process?
>Something usable in a SQL SELECT statement?


You can create your own function to retrieve words until you reach the maximum length. Try something like:
SET STEP ON
CREATE TABLE myTable ( cMemoField m )
INSERT INTO myTable VALUES("This is a test of the emergency broadcast system.  This is only a test.")
INSERT INTO myTable VALUES("Yesterday was the day before today, which could be the last day of the rest of my life.")

SELECT CAST(getUnbrokenWordsToCharCount(cMemoField,50) AS c(200)) AS cField ;
    FROM myTable ;
    INTO CURSOR c_list

BROWSE NOWAIT
CANCEL

FUNCTION getUnbrokenWordsToCharCount
LPARAMETERS tcMemo, tnMaxLength
LOCAL lnI, lcOutput, lcWord

    * Remove any CR/LF
    tcMemo = CHRTRAN(tcMemo, CHR(13)+CHR(10), SPACE(1))

    * Initialize our return string
    lcOutput = SPACE(0)
    FOR lnI = 1 to GETWORDCOUNT(tcMemo)
        * Grab this word
        lcWord = GETWORDNUM(tcMemo, lnI)
        
        * Will it be too much?
        IF LEN(lcOutput) + LEN(lcWord) > tnMaxLength
            * All done
            RETURN lcOutput
        ENDIF
        
        * Concatenate
        lcOutput = lcOutput + IIF(EMPTY(lcOutput), SPACE(0), SPACE(1)) + lcWord
    NEXT
    * The entire string was usable
    RETURN lcOutput
Previous
Reply
Map
View

Click here to load this message in the networking platform