textoidl_str_replace.pro 1.48 KB
FUNCTION TEXTOIDL_STR_REPLACE, str, outstring, instring
;+
; NAME:
;     STR_REPLACE
; PURPOSE:
;     replace a substring by another in an input string
; CALLING SEQUENCE:
;     result = STR_REPLACE(str, outstring, instring)
; INPUT:
;     str : input string
;     outstring : substring to be searched for and replaced
;     instring : substring to be put in str in replacement of outstring
; OUTPUT:
;     input string with outstring replaced by instring
; KEYWORDS:
;     none
; EXAMPLES:
;     IDL> str = 'tralala tsoin tsoin'
;     IDL> PRINT, STR_REPLACE(str, 'tsoin', 'ploum')
;     (IDL prints 'tralala ploum ploum')
; ALGORITHM:
;     simple string manipulation
; SIDE EFFECTS:
;     none
; RESTRICTIONS:
;     Report any bugs to Jacques Delabrouille
;     j.delabrouille@cdf.in2p3.fr
; PROCEDURES CALLED:
;     none
; REVISION HISTORY
;     Written, Jacques Delabrouille June 2000
;     Bug (infinite loop) when instring contains outstring fixed, JD, May 2001 
;-


IF ( N_PARAMS() EQ 0 ) THEN BEGIN
	print,'  CALL IS : result = STR_REPLACE(str, outstring, instring)'
;	print,'  Keywords :'
     GOTO, closing
ENDIF

search = 1

begin_string = ''
endstring = str

WHILE search EQ 1 DO BEGIN
  whout = STRPOS(endstring,outstring)
  IF whout EQ -1 THEN BEGIN
     RETURN, begin_string+endstring
  	GOTO, closing
  ENDIF ELSE BEGIN
    begin_string = begin_string + STRMID(endstring,0,whout) + instring
    endstring = STRMID(endstring,whout+STRLEN(outstring))
  ENDELSE
ENDWHILE
RETURN, newstr

closing:
END