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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code needed for ALLTRIMCR() function

Status
Not open for further replies.

mikemck7

Technical User
Dec 17, 2003
38
I'm designing reports in VFP8 using data contained in several large tables. Some of the data has to be parsed and extracted from memo fields.

Numerous records contain leading and trailing carriage returns causing MEMLINES() to return misleading results. Others contain nothing but carriage returns thereby returning .F. when tested for content with EMPTY().

I've been toying around with something I call ALLTRIMCR() but haven't gotten it quite right. I want it to get rid of all leading and trailing carriage returns in a manner similar to the way ALLTRIM() works with spaces. Suggestions welcome.

Mike
 
Hi:
use strstran() function. it think it will do the trick.

* Replace line feed with ' '
c_cooked = strtran(MEMOFIELD,chr(10),' ')
* replace carriage return with ' '
c_cooked = strtran(c_cooked,chr(13),' ')

* replace double space/leading/trailing blanks
c_cooked = alltrim(c_cooked)
c_cooked = strtran(c_cooked,' ',' ')

try it

Nasib Kalsi
 
And here's one for any version:

Code:
FUNCTION alltrimcr( tcInString )
IF tcInstring = STRTRAN( tcInString, CHR(13)+CHR(10), '')
  *** Contains only CRLF
  RETURN STRTRAN( tcInString, CHR(13)+CHR(10), '')
ENDIF

*** Now check the end of the string
lcStr = ALLTRIM( tcInstring )
DO WHILE .T.
  *** Strip off last character if CR/LF
  IF INLIST( RIGHT( tcInstring, 1 ), CHR(13), CHR(10) )
    lcStr = LEFT( lcStr, LEN( lcStr ) - 1 )
  ELSE
    EXIT
  ENDIF
ENDDO
RETURN lcStr



----
Andy Kramek
Visual FoxPro MVP
 
If you want to have some function like ALLTRIM() but to remove CR/LF (VFP9 version). Based on Mike's code
Code:
FUNCTION ALLTRIMCR(tcString)
RETURN ALLTRIM(ALLTRIM(tcString, 0, CHR(13)), 0, CHR(10))

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
I revisited my ALLTRIMCR() function and came up with the code below. I needed the function to also catch any empty spaces between two CRLFs. I also did not want CRLFs within the "good" part of the string to be touched so I did not use STRTRAN(). To demonstrate, paste entire code in a program file and run.

Code:
*-* Program to demonstrate the use of my ALLTRIMCR() function.

*-* Define separate constants for CHR(13) and CHR(10)
#define CR CHR(13)
#define LF CHR(10)
*-* And a single constant for CHR(13)+CHR(10)
#define CRLF CHR(13)+CHR(10)

*-* Construct sample string for testing ALLTRIMCR()
cMyStr = CRLF + CRLF + CRLF + CRLF + ;
SPACE(8) + "TEK-TIPS VFP FORUMS" + CRLF + CRLF + "Microsoft: VFP" + ;
CRLF + " - Automation, Mail & 3rd Party Svcs" + CRLF + CRLF + ;
"Microsoft: VFP" + CRLF + ;
" - Databases, SQL&VFP, and Reports" + CRLF + CRLF + ;
"Microsoft: VFP" + CRLF + ;
" - Forms, Classes and Controls" + CRLF+CRLF + ;
"Microsoft: VFP" + CRLF + ;
" - General Coding Issues" + CRLF+CRLF + ;
SPACE(3)+ CRLF + CRLF + SPACE(4)

*-* Show what the string looks like before triming
WAIT WINDOW cMyStr

*-* Send sample string to ALLTRIMCR() for trimming.
cMyStr=ALLTRIMCR(cMyStr)

*-* Show what the string looks like after triming
WAIT WINDOW cMyStr

*-* ALLTRIMCR() - This function removes leading and trailing 
*-*	carriage returns, line feeds and spaces from any string passed.
*-* It does not change anything in between. It returns an empty
*-* string if an empty string or one with only CRs and LFs is passed to it.

FUNCTION ALLTRIMCR
LPARAMETERS tcString
*-* Eliminate leading carriage returns, line feeds and spaces
DO WHILE .t.
	IF AT(CR,tcString)=1 OR AT(LF,tcString)=1
		tcString=ALLTRIM(right(tcString,LEN(tcString)-1))
	ELSE
		EXIT	
	ENDIF
ENDDO
*-* Eliminate trailing carriage returns, line feeds and spaces
DO WHILE .t.
	IF RIGHT(tcString,1)= CR OR RIGHT(tcString,1)=LF
		tcString=ALLTRIM(LEFT(tcString,LEN(tcString)-1))
	ELSE
		EXIT	
	ENDIF
ENDDO
RETURN tcString
 
Hi MikeMck7:

Sorry, I did not understood your problem. You do not need the while loops. Thanks you Mike Lewis for the pointer.

just put

function alltrimcr(txString)
* remove trailaing and leading spaces,CR,LF and Tabs
return alltrim(tcString,chr(10),chr(13),chr(32),chr(9))

will do the trick ...


Nasib Kalsi
 
Nasib,

Multiple arguments in ALLTRIM() is new in VFP9. Unfortunately, as I mentioned in my original post, I'm using VFP8.

SIDEBAR:
There is an error in the sample code I posted above to demonstrate the use of my ALLTRIMCR() function. Altough the sample program will run as is, the #DEFINE statments used to create CR and LF preprocessor directives rightly belong in the function's code. Left unchanged, the function will fail if called independently.

Thanks,

Mike



 
oops again. Your solution is the way to go. But if you have lots of data to process then the following may help run it faster.

procedure ltrimcr
lparameter tcString
local ii && use for index and loops

* ltrimcr() example
* Higher occurence first, rearrange as required
#define __TRIMCHAR chr(10)+chr(13)+chr(32)+chr(9)

ii = 1
do while at(substr(tcString,ii,1),__TRIMCHAR) > 0
ii = ii + 1
enddo

if ii > 1
* do not need this 'if statement'. i think it is faster
* than memory swap. you can try with/without and
* check for speed.
tcString = substr(tcString,ii)
endif

return tcString

i started with vfp6(a bit) and next one was vfp9, am still in process.

Nasib Kalsi
 
Nasib,
I like your approach also. It seems tighter than mine but I wasn't really concerned about speed in this case since I'm not expecting to use it that often. I ran my code against one of the larger tables and it was over in a flash.

You didn't deal with possible trailing codes in yours so I added another piece as shown. I owe you one though because I hadn't thought about tabs until you mentioned it. Thanks.

Code:
[COLOR=green]*-* *************************************
*-* Remove leading and trailing spaces,
*-* tabs, carriage returns and line feeds[/color]
FUNCTION ltrimcr[COLOR=green]
*-* *************************************[/color]
LPARAMETER tcString
LOCAL ii, jj
#DEFINE __TRIMCHAR CHR(10)+CHR(13)+CHR(32)+CHR(9)
[COLOR=green]
*-* Remove leading ...[/color]
ii = 1
DO WHILE AT(SUBSTR(tcString,ii,1),__TRIMCHAR) > 0
	ii =  ii + 1
ENDDO
IF ii > 1
	tcString = SUBSTR(tcString,ii)
ENDIF
[COLOR=green]
*-* Remove traling ...[/color]
ii = 0
jj= LEN(tcString)
DO WHILE RIGHT(LEFT(tcString,jj-ii),1) $ __TRIMCHAR
	ii =  ii + 1
ENDDO
tcString= LEFT(tcString,LEN(tcString)-ii)
RETURN tcString
 
Hi Mike:

function rtrimcr(tcString)

#define __TRIMCHAR chr(10)+chr(13)+chr(32)+chr(9)

local ii

ii = len(tcString)

if ii > 0
while at(substr(tcString,ii,1),__TRIMCHAR) > 0
ii = ii - 1
enddo
tcString = substr(tcString,1,ii)
endif

return tcString

* Not tested. I knew rtrimcr() is similar but a bit
* trickier, so I left it for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top