Focal Point
Replacing multiple occurences of a value with another within a string

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/7871077331

April 21, 2004, 01:48 PM
jif26
Replacing multiple occurences of a value with another within a string
Is there an easy way in focus to replace multiple occurrences of a string within one string ?

Here's what I want to do :

From an alpha field, I want to define another one that replaces all spaces in the field with " ".

How can I achieve this ?
April 21, 2004, 04:41 PM
Mikel
You can use CTRAN subroutine to translate one character to another (not string)


-SET &STRING = 'AB CD EF' ;
-SET &STRING = CTRAN(&STRING.LENGTH, &STRING, 32, 95, &STRING);

For a unique string replacement with known position, WebFOCUS also has the OVRLAY subroutine .

Regards,
Mikel

This message has been edited. Last edited by: <Mabel>,
April 21, 2004, 04:55 PM
jif26
Yes, I know that, but this doesn't help me with my issue.
April 22, 2004, 10:21 PM
Mikel
Only available in MAINTAIN, STRAN function allows substitution of substrings.


MAINTAIN FILE CAR
MODULE IMPORT (MNTUWS);
COMPUTE STRING1/A10 = 'AB CD EF' ;
COMPUTE SUBSTR1/A1 = ' ' ;
COMPUTE SUBSTR2/A2 = 'XXX' ;
COMPUTE STRING2/A20 = STRAN(STRING1, SUBSTR1, SUBSTR2) ;
TYPE "OLD STRING: TYPE "NEW STRING: END
-RUN

-* Output:
-* OLD STRING: AB CD EF
-* NEW STRING: ABXXCDXXEFXXXX
Regards,
Mikel

This message has been edited. Last edited by: <Mabel>,
April 28, 2004, 02:02 PM
<Pietro De Santis>
Too bad you haven't moved to WebFOCUS 5.2 yet, as there's a new function called SQUEEZ that will "reduce multiple contiguous spaces within a character string to a single space".

Cheers,

Pietro.
June 01, 2007, 06:28 PM
dballest
I have a similar issue with jif26. I need to replace any occurances of < with < and > with >

OVRLAY works but you need to loop if there are more than 1 occurances of the string.

Is there an easier way? Single pass?

Before: abc<123>def>ghi
After: abc<123>def>ghi

Thanks,


Dev: WebFOCUS 7.6.10, Data Migrator 7.6.10
QA: WebFOCUS 7.6.10, Data Migrator 7.6.10
Prod: WebFOCUS 7.6.2, Data Migrator 7.6.8
Windows 2K3, Tomcat 5.5.17, IIS 6
Usage: HTML, PDF, Excel, Self-serve, BID and MRE
June 02, 2007, 03:20 AM
Alan B
Use STRREP.
It's useful to know what area you are using, e.g. TABLE,DM,MAINTAIN.

Change < to & lt; and > to & gt;
-SET &STRING='abc<123>def>ghi';
-SET &ASTRING = STRREP(&STRING.LENGTH,&STRING,1,'<',4,'&|lt;',&STRING.LENGTH*2,'A&STRING.LENGTH*2');
-SET &BSTRING = STRREP(&ASTRING.LENGTH,&ASTRING,1,'>',4,'&|gt;',&ASTRING.LENGTH*2,'A&ASTRING.LENGTH*2');
-TYPE &BSTRING



Alan.
WF 7.705/8.007
June 04, 2007, 12:14 PM
dballest
Hi Alan B,

I'm using TABLE and/or DEFINE. We have fields that have multiple occurance of these characters and we want them replaced.

Thanks,


Dev: WebFOCUS 7.6.10, Data Migrator 7.6.10
QA: WebFOCUS 7.6.10, Data Migrator 7.6.10
Prod: WebFOCUS 7.6.2, Data Migrator 7.6.8
Windows 2K3, Tomcat 5.5.17, IIS 6
Usage: HTML, PDF, Excel, Self-serve, BID and MRE
June 04, 2007, 12:20 PM
Alan B
Same difference really:
STRING/A20='abc<123>def>ghi';
ASTRING/A100V = STRREP(20,STRING,1,'<',4,'&|lt;',100,ASTRING);
BSTRING/A100V = STRREP(LENV(ASTRING,'I2'),ASTRING,1,'>',4,'&|gt;',100,BSTRING);



Alan.
WF 7.705/8.007
June 04, 2007, 12:52 PM
dballest
I tried the code below and it did not work:

DEFINE FILE CAR
STRING/A20='abc<123>def>ghi';
4,'&|lt;',100,ASTR);
ASTRING/A100V = STRREP(20,STRING,1,'<',4,'&|lt;',100,ASTRING);
BSTRING/A100V = STRREP(LENV(ASTRING,'I2'),ASTRING,1,'>',4,'&|gt;',100,BSTRING);
END

TABLE FILE CAR
PRINT
COUNTRY
STRING
ASTRING
BSTRING
END

Tried opening the DEFINE section in the physical view in Dev studio and when I click okay, I get: "Semicolons are not allowed" and won't allow me to save.


Dev: WebFOCUS 7.6.10, Data Migrator 7.6.10
QA: WebFOCUS 7.6.10, Data Migrator 7.6.10
Prod: WebFOCUS 7.6.2, Data Migrator 7.6.8
Windows 2K3, Tomcat 5.5.17, IIS 6
Usage: HTML, PDF, Excel, Self-serve, BID and MRE
June 04, 2007, 01:04 PM
dballest
Correction:

DEFINE FILE CAR
STRING/A20='abc<123>def>ghi';
ASTRING/A100V = STRREP(20,STRING,1,'<',4,'&|lt;',100,ASTRING);
BSTRING/A100V = STRREP(LENV(ASTRING,'I2'),ASTRING,1,'>',4,'&|gt;',100,BSTRING);
END

TABLE FILE CAR
PRINT
COUNTRY
STRING
ASTRING
BSTRING
END


Dev: WebFOCUS 7.6.10, Data Migrator 7.6.10
QA: WebFOCUS 7.6.10, Data Migrator 7.6.10
Prod: WebFOCUS 7.6.2, Data Migrator 7.6.8
Windows 2K3, Tomcat 5.5.17, IIS 6
Usage: HTML, PDF, Excel, Self-serve, BID and MRE
June 04, 2007, 01:05 PM
Alan B
Dev studio opens the code as I wrote it, and it works as I tested it.

What error are you getting.


Alan.
WF 7.705/8.007
June 04, 2007, 01:10 PM
dballest
I can open the code as using text editor in Dev studio. It is only when I use, not sure what you call it, the DEFINE gui and not do anything and click the OK button. I get "Semicolons not allowed.". If you need a screen shot of the message, I can send it to you.


Dev: WebFOCUS 7.6.10, Data Migrator 7.6.10
QA: WebFOCUS 7.6.10, Data Migrator 7.6.10
Prod: WebFOCUS 7.6.2, Data Migrator 7.6.8
Windows 2K3, Tomcat 5.5.17, IIS 6
Usage: HTML, PDF, Excel, Self-serve, BID and MRE
June 04, 2007, 01:28 PM
Alan B
That would be the DEFINE tool objecting to a semi colon in the DEFINE.

Not much you can do about that, use the text editor, temporarily remove the semi colons, work in the gui tool, and put back when you have finished.

Does the code itself run and produce correct results?


Alan.
WF 7.705/8.007
June 04, 2007, 01:29 PM
dballest
Running the code below did not work. The strings < and > were not replaced.

DEFINE FILE CAR
STRING/A20='abc<123>def>ghi';
ASTRING/A100V = STRREP(20,STRING,1,'<',4,'&|lt;',100,ASTRING);
BSTRING/A100V = STRREP(LENV(ASTRING,'I2'),ASTRING,1,'>',4,'&|gt;',100,BSTRING);
END

TABLE FILE CAR
PRINT
COUNTRY
STRING
ASTRING
BSTRING
END


Dev: WebFOCUS 7.6.10, Data Migrator 7.6.10
QA: WebFOCUS 7.6.10, Data Migrator 7.6.10
Prod: WebFOCUS 7.6.2, Data Migrator 7.6.8
Windows 2K3, Tomcat 5.5.17, IIS 6
Usage: HTML, PDF, Excel, Self-serve, BID and MRE
June 04, 2007, 02:06 PM
Alan B
The strings will be replaced, but when you view it under HTML, the & lt; will show as < and the & gt; will show as >. Which is expected.

View source will show the difference.

If this is not what you want, please explain as your original message was a bit unclear.


Alan.
WF 7.705/8.007
June 04, 2007, 02:28 PM
dballest
You are correct! This is what we need.

Thanks!


Dev: WebFOCUS 7.6.10, Data Migrator 7.6.10
QA: WebFOCUS 7.6.10, Data Migrator 7.6.10
Prod: WebFOCUS 7.6.2, Data Migrator 7.6.8
Windows 2K3, Tomcat 5.5.17, IIS 6
Usage: HTML, PDF, Excel, Self-serve, BID and MRE