REPLACE$ function REPLACE statement

Purpose: The REPLACE$ function returns a string that is the MainStr$ parameter in which the Match$ substrings have been replaced by the Change$ string.

The REPLACE statement does not return a string. Instead it directly replaces Match$ with Change$ in MainStr$.


 Syntax 1:

 SubStr$ = REPLACE$(MainStr$, Match$, Change$)

 Parameters:

  • SubStr$ Returned string with all occurences of Match$ replaced by Change$.
  • MainStr$ String in which Match$ character is to be searched for and replaced with Change$.
  • Match$ Occurences of this string are to be replaced in MainStr$.
  • Change$ String to replace Match$. Change$ can not contain ASCII code 0.
 Syntax 2:

 REPLACE Match$ WITH Change$ IN MainStr$

 Parameters:

  • Match$ Occurences of this string are to be replaced in MainStr$.
  • Change$ String to replace Match$. Change$ can not contain ASCII code 0.
  • MainStr$ String in which Match$ character is to be searched for and replaced with Change$.

The REPLACE command also allows full expressions and arrays as arguments.


 Syntax 3:

 REPLACE UCASE$(A$) WITH LCASE$(B$) IN STRIM$(C$)

Example:


 SubStr$ = REPLACE$("ABCDEFG","DEF","123")

Result:

SubStr$ will equal "ABC123G"

NOTE: Replacing a character with ASCII 0 will not work. For example,


 Filter$ = "Icons(*.ico)|*.ICO|All files(*.*)|*.*"
 REPLACE "|" WITH CHR$(0) IN Filter$

will not work.

Something like the following must be used instead.


 DIM RAW p AS CHAR PTR, Filter$[40] AS CHAR
 Filter$ = "Icons(*.ico)|*.ICO|All files(*.*)|*.*"
 p=Filter
 WHILE *p
    IF *p=124 THEN *p=0
    p++
 WEND

BCX Console Sample Programs using REPLACE$ function.

S20.bas   S103.bas

IREPLACE$ function IREPLACE statement

IREPLACE$ and IREPLACE perform a case insensitive replacement of the Match$ substrings contained in MainStr$.


 Syntax 1:

 SubStr$ = IREPLACE$(MainStr$, Match$, Change$)

 Parameters:

  • SubStr$ Returned string with all case insensitive occurences of Match$ replaced by Change$.
  • MainStr$ String in which Match$ character is to be searched for and replaced with Change$.
  • Match$ Occurences of this string are to be replaced without regard to case sensitivity in MainStr$. ABC, abc, Abc, aBc and so on are all considered equivalent.
  • Change$ String to replace Match$. Change$ can not contain ASCII code 0.
 Syntax 2:

 IREPLACE Match$ WITH Change$ IN MainStr$

 Parameters:

  • Match$ Occurences of this string are to be replaced without regard to case sensitivity in MainStr$. ABC, abc, Abc, aBc and so on are all considered equivalent.
  • Change$ String to replace Match$. Change$ can not contain ASCII code 0.
  • MainStr$ String in which Match$ character is to be searched for and replaced with Change$.

The IREPLACE command also allows full expressions and arrays as arguments.


 Syntax 3:

 IREPLACE UCASE$(A$) WITH LCASE$(B$) IN STRIM$(C$)

Here is a simple example.


 DIM A$

 A$= "This is A saMpLe oF a LONG, long, LOng, loNG, rambling sentence."

 IREMOVE "long, " FROM A$ 'now we can remove them all
 IREPLACE CHR$(32,32) WITH CHR$(32) IN A$
 IREPLACE "A SAMPLE OF" WITH "not" IN A$
 PRINT A$ ' Result: This is not a rambling sentence.