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:
Parameters: |
DIM NewStr$ NewStr$ = REPLACE$("ABCDEFG","DEF","123") PRINT NewStr$
ABC123G
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: |
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$ 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:
Parameters:
|
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:
|
The IREPLACE statement also allows full expressions and arrays as arguments.
IREPLACE UCASE$(A$) WITH LCASE$(B$) IN STRIM$(C$)
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$
This is not a rambling sentence.
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:
Parameters:
|
DIM A$ A$ = "9ndz0in035707ndz0357016v70y6u039k70is07qu910360357016v70y6u0m9k7." PRINT A$, " <<-- Original string" PRINT REPLACEANY$(A$, "069274513z", " oaceghlt,"), " <<-- Case-SENSITIVE replacement" PAUSE
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 . . .
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:
Parameters:
|
DIM A$ A$ = "9ndz0in035707ndz0357016v70y6u039k70is07qu910360357016v70y6u0m9k7." PRINT A$, " <<-- Original string" PRINT IREPLACEANY$(A$, "069274513z", " OACEGHLT,"), " <<-- Case-insensitive replacement" PAUSE
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 . . .