REPLACE$ function

Purpose:

The REPLACE$ function returns a string in which the Match substrings of MainStr have been replaced by the Change string.

Syntax:

NewStr = REPLACE$(MainStr AS STRING, _
                    Match AS STRING, _
                   Change AS STRING)

Return Value:

  • Data type: STRING
    NewStr string with all occurrences in MainStr of Match replaced by Change.

Parameters:

  • Data type: STRING
    MainStr String in which Match character is to be searched for and replaced with Change.
  • Data type: STRING
    Match occurrences of this string are to be replaced in MainStr.
  • Data type: STRING
    Change String to replace Match. Change cannot contain ASCII code 0.

Example:

DIM NewStr$

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

PRINT NewStr$

Result:

ABC123G

BCX Console Sample Programs using the REPLACE$ function.

REPLACE statement

Purpose:

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

Syntax:

REPLACE Match AS STRING WITH Change AS STRING IN MainStr AS STRING

Parameters:

  • Data type: STRING
    Match occurrences of this string are to be replaced in MainStr.
  • Data type: STRING
    Change String to replace Match. Change cannot contain ASCII code 0.
  • Data type: STRING
    MainStr String in which Match character is to be searched for and replaced with Change.

Remarks:

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

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

👉 String concatenation using & or + within a REPLACE statement requires that the concatenation is wrapped with parentheses as in the following example.

REPLACE CHR$(9) WITH (CHR$(32) & CHR$(32)) IN MainStr$

👉 Trying to use REPLACE to replace 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 may 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

IREPLACE$ function

Purpose:

IREPLACE$ returns a string resulting from the case insensitive replacement of the Match substrings contained in MainStr.

Syntax:

NewStr = IREPLACE$(MainStr AS STRING, Match AS STRING, Change AS STRING)

Return Value:

  • Data type: STRING
    NewStr string with all case insensitive occurrences of Match replaced by Change.

Parameters:

  • Data type: STRING
    MainStr String in which Match character is to be searched for and replaced with Change.
  • Data type: STRING
    Match occurrences 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.
  • Data type: STRING
    Change String to replace Match. Change cannot contain ASCII code 0.

IREPLACE statement

Purpose:

IREPLACE performs an in place, case insensitive, replacement of the Match substrings contained in MainStr.

Syntax:

IREPLACE Match AS STRING WITH Change AS STRING IN MainStr AS STRING

Parameters:

  • Data type: STRING
    Match occurrences 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.
  • Data type: STRING
    Change String to replace Match. Change cannot contain ASCII code 0.
  • Data type: STRING
    MainStr String in which Match character is to be searched for and replaced with Change.

Remarks:

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

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

Example:

DIM A$

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

IREMOVE "long, " FROM A$
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.

REPLACEANY$ function

Purpose:

The REPLACEANY$ function returns a string with all occurrences in MainStr of characters in MatchChars replaced by the corresponding, case-sensitive, at position, characters in ReplaceChars.

Syntax:

NewStr = REPLACEANY$(MainStr AS STRING, _
                  MatchChars AS STRING, _
                ReplaceChars AS STRING)

Return Value:

  • Data type: STRING
    NewStr string with all occurrences in MainStr of characters in MatchChars replaced by the corresponding, case-sensitive, at position, characters in ReplaceChars.

Parameters:

  • Data type: STRING
    MainStr String to be searched for MatchChars characters and replaced with ReplaceChars characters.
  • Data type: STRING
    MatchChars occurrences of characters in this string are to be replaced by the corresponding, case-sensitive, at position, characters in MainStr.
  • Data type: STRING
    ReplaceChars occurrences of characters in this string will replace the corresponding, case-sensitive, at position, MatchChars characters in MainStr.

Example:

DIM A$
A$ = "9ndz0in035707ndz0357016v70y6u039k70is07qu910360357016v70y6u0m9k7."

PRINT A$,                                           " <<-- Original string"

PRINT REPLACEANY$(A$, "069274513z", " oaceghlt,"),  " <<-- Case-SENSITIVE   replacement"

PAUSE

Result:

9ndz0in035707ndz0357016v70y6u039k70is07qu910360357016v70y6u0m9k7. <<-- Original string
and, in the end, the love you take is equal to the love you make. <<-- Case-SENSITIVE   replacement

Press any key to continue . . .

IREPLACEANY$ functions

Purpose:

The IREPLACEANY$ function returns a string with all occurrences in MainStr of characters in MatchChars replaced by the corresponding, case-insensitive, at position, characters in ReplaceChars.

Syntax:

NewStr = IREPLACEANY$(MainStr AS STRING, _
                   MatchChars AS STRING, _
                 ReplaceChars AS STRING)

Return Value:

  • Data type: STRING
    NewStr string with all occurrences in MainStr of characters in MatchChars replaced by the corresponding, case-insensitive, at position, characters in ReplaceChars.

Parameters:

  • Data type: STRING
    MainStr String to be searched for MatchChars characters and replaced with ReplaceChars characters.
  • Data type: STRING
    MatchChars occurrences of characters in this string are to be replaced by the corresponding, case-insensitive, at position, characters in MainStr.
  • Data type: STRING
    ReplaceChars occurrences of characters in this string will replace the corresponding, case-insensitive, at position, MatchChars characters in MainStr.

Example:

DIM A$
A$ = "9ndz0in035707ndz0357016v70y6u039k70is07qu910360357016v70y6u0m9k7."

PRINT A$,                                           " <<-- Original string"

PRINT IREPLACEANY$(A$, "069274513z", " OACEGHLT,"), " <<-- Case-insensitive replacement"

PAUSE

Result:

9ndz0in035707ndz0357016v70y6u039k70is07qu910360357016v70y6u0m9k7. <<-- Original string
And, in THE End, THE LOvE yOu TAkE is EquAL TO THE LOvE yOu mAkE. <<-- Case-insensitive replacement

Press any key to continue . . .