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

Multiple Replace Statements for String 4

Status
Not open for further replies.

dunkyn

Technical User
Apr 30, 2001
194
US
I want to remove some repetitive abbreviations from a string of text. I have placed multiple replace statements in the same formula. It doesn't seem to be working in all instances. Looks like it is only recognizing the last replace statement.

Sample below:

Replace ({table.field1},"INC COM","");
Replace ({table.field1}, " INC" , "");
Replace ({table.field1}, " CORP" , "");
Replace ({table.field1}, " CORP COM" , "");
Replace ({table.field1}, " CO COM" , "");
Replace ({table.field1}, " CO " , "");
Replace ({table.field1}, "COM" , "");


There must be a better way ?
 
Use something like:
---------------------------------------
StringVar HoldString;

HoldString := Replace ({table.field1},"INC COM","");

HoldString := Replace (HoldString, " INC" , "");

HoldString := Replace (HoldString, " CORP" , "");

HoldString := Replace (HoldString, " CORP COM" , "");

HoldString := Replace (HoldString, " CO COM" , "");

HoldString := Replace (HoldString, " CO " , "");

HoldString := Replace (HoldString, "COM" , "");
-------------------------------------------------

Cheers,
- Ido

CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
IDO has done it correctly....the reason yours would not work is that

Replace ({table.field1},"INC COM","");

Does not update the value of {table.field1} .... it would only display {table.field1} with "INC COM" removed....by assigning it to a variable as he has done and then displaying the variable at the end...you get the desired result AND can use it elsewhere in the report.

I would only add this tiny change...for readability and timing only

WhilePrintingRecords;
StringVar HoldString;

HoldString := Replace ({table.field1},"INC COM","");

HoldString := Replace (HoldString, " INC" , "");

HoldString := Replace (HoldString, " CORP" , "");

HoldString := Replace (HoldString, " CORP COM" , "");

HoldString := Replace (HoldString, " CO COM" , "");

HoldString := Replace (HoldString, " CO " , "");

HoldString := Replace (HoldString, "COM" , "");

HoldString;


Jim Broadbent
 
Don't be cowards, give yourselves paren headaches!!!

Replace(Replace(Replace(Replace(Replace(Replace(Replace({table.field1},"INC COM","")," INC","")," CORP","")," CORP COM","")," CO COM","")," CO ",""),"COM","")

-k
 
Jim, I don't believe there is any scenario under which WhilePrintingRecords would be necessary in this case. I know that some report developers throw that expression in "just for good measure" but it's not always wise.

Kai, I like the spirit but I always teach my students that when having a choice between a formula that is "easy to debug" (incremental in this case) or a formula that is "compact" or "efficient", they should almost always go with "easy to debug".

Just my own biases... :eek:)

Cheers,
- Ido

CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
<chucklin>

You're faaaar too easy on 'em, Ido, I should have slipped in a Wwitch for good measure ;)

-k
 
IDO....I said that this was a minor change....the more Major Minor change was to finish with the resulting string by itself.

I did not say this was serious...however...in defense of my action...Adding WhilePrintingRecords...never harms and may help....the thing is that we do NOT know the circumstances of the use of this formula so in truth we do not know :)

And SV....that formula is not very readable and I doubt very much if it is faster than that described by IDO

Jim Broadbent
 
Jim,

I believe that adding WhilePrintingRecords would block the resulting formula from participating in any record selection criteria. Just an example of why throwing it in &quot;just for good measure&quot; is not always the correct thing to do.

On the other hand, I fully understand why many report developers are adding the darn thing almost automatically. They've wasted too many hours hunting down a problem due to NOT having it in... :eek:)
I just think that in this case there is no reason to use it (and some reasons for not using it).

By the way, I assume you are suggesting adding the variable on its own at the end of the formula just as some sort of clarification. That added step shouldn't make a difference in outcome.

Cheers,
- Ido

CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
Thank you all. The HoldString is new to me. I'll have to try it. The nested replace works too, but I like the readability of the HoldString.

You guys are great. Thanks a bunch!
 
Jim: just a little levity...

Ido: I meant to say I should have tossed in a few Switches ;)

-k
 
IDO - IT IS OBVIOUS - that you never put &quot;WhilePrintingRecords&quot; into a record selection formula...after all...read the name of the function

The formula in question is being used for manipulation of existing data....for future use or current printing, it is unclear, but definately NOT for record selection.

I will give you that perhaps...though very unlikely...it may be used for grouping data and in which case you are correct...this function should not be used there either.

My real purpose of making the addition to your post was not to correct it....but rather to explain the reason for its use. You presented an answer without an explanation as to why the user's approach was wrong...hence he would be prone to make a similar mistake in the future...

SV - the levity was misplaced here

Jim Broadbent
 
Ido: The use of any variable appears to block it's use (bby that I assume you mean passing the result on to the database) in the record selection formula.

-k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top