;+ ; NAME: ; STRREPLACE ; ; PURPOSE: ; The STRREPLACE procedure replaces the contents of one string ; with another. The first occurrence of the search substring, Find ; within the source string, String is replaced by the string, ; Replacement. ; ; CATEGORY: ; String Processing. ; ; CALLING SEQUENCE: ; ; STRREPLACE, String, Find, Replacement ; ; INPUTS: ; String: The string to have substring(s) replaced. If String is ; an array, Find is replaced by Replacement in the first ; occurrence of Find of every element of the array. ; ; Find: The scalar substring to be replaced. If this argument is ; not a string, it is converted using IDL's default ; formatting rules. ; ; Replacement: A scalar string to replace the Find substring. If ; this argument is not a string, it is converted using IDL's ; default formattting rules. ; ; EXAMPLE: ; ; If the variable A contains the string "IBM is fun", the ; substring "IBM" can be replaced with the string "Microsoft" ; by entering: ; ; STRREPLACE, A, 'IBM', 'Microsoft' ; ; MODIFICATION HISTORY: ; Written by: Han Wen, June 1995. ; Revised to handle multiple replacements - Brian Templeman 2011 ;- pro strreplace, Strings, Find1, Replacement1 IF Find1 EQ Replacement1 THEN RETURN ; Check integrity of input parameter NP = N_PARAMS() if (NP ne 3) then PRINT,'Must be called with 3 parameters, '+$ 'Strings, Find, Replacement' stringtype = SIZE(Strings, /TYPE) num_strings = SIZE(Strings, /N_ELEMENTS) if (stringtype[0] ne 7) then PRINT,'Parameter must be of string type.' Find = STRING(Find1) pos = STRPOS(Strings[0], Find) FOR i = 0, num_strings -1 DO BEGIN WHILE( pos GT 0 ) DO BEGIN Replacement = STRING(Replacement1[i]) Flen = STRLEN(Find) prefix = STRMID(Strings(i),0,pos) suffix = STRMID(Strings(i),pos+Flen, STRLEN(Strings(i))-(pos + Flen)) Strings(i) = prefix + replacement + suffix pos = STRPOS(Strings[i], Find) ENDWHILE ENDFOR END