;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       StringReplace() 
;; 
;;ACTION         Replaces text substrings within a string 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2009/08/29 
;; 
;;HISTORY        1.0  - 2009/08/29 - Initial Release 
;; 
;;SYNTAX         StringReplace(Src, S1, S2 [, Start] [, Count]) 
;; 
;;PARAMETERS     Src - REQUIRED - String 
;;               - The original string where replacements will be performed 
;; 
;;               S1 - REQUIRED - String 
;;               - The substring to search for 
;; 
;;               S2 - REQUIRED - String 
;;               - The string that will replace S1 
;; 
;;               Start - OPTIONAL - Integer 
;;               - The occurrence within the source string to begin the replacement with. 
;;		 Defaults to "1". 
;; 
;;               Count - OPTIONAL - Integer 
;;               - The number of string replacements to perform.  
;;		 All occurrences will be replaced if not specified. 
;; 
;;REMARKS        Used to simplify the use of Join(Split()) constructs when replacing 
;;               one text string with another 
;; 
;;RETURNS        The original Src string with occurrences of S1 replaced with S2 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3, W2K8, Vista 
;; 
;;EXAMPLES        
;;               ; replace all 
;;               StringReplace('text string with another string in the string text', 'string', 'STRING') ? 
;;               ; replace all, starting with the second occurrence 
;;               StringReplace('text string with another string in the string text', 'string', 'STRING', 2) ? 
;;               ; replace just the second occurrence 
;;               StringReplace('text string with another string in the string text', 'string', 'STRING', 2, 1) ? 
; 
Function StringReplace($_Source, $_S1, $_S2, Optional $_Start, Optional $_Count)
 
  Dim $_Case						; current CASE setting 
  Dim $_aSource						; array of source string components 
  Dim $_I						; index pointer 
  Dim $_Min, $_Max					; min and max process values 
 
  $_Case = SetOption('CaseSensitivity', 'On')		; insure case-specific matching 
 
  $_aSource = Split($_Source, $_S1)			; break source sting into array 
  $_Min = 0						; Start processing here 
  $_Max = UBound($_aSource)				; end processing here 
  $_Start = IIf($_Start, $_Start - 1, 0)		; make the supplied start value 0-based 
 
  ; define the Count value to ALL if not specified 
  $_Count = IIf($_Count, $_Count, $_Max)
  If $_Count > $_Max
    $_Count = $_Max					; don't allow a greater count than elements 
  EndIf
 
  ; handle situations where the first element is a replacement value 
  If $_aSource[0] = ''
    If $_Start = 0 And $_Count				; see if we should replace based on start and count 
      $StringReplace = $StringReplace + $_S2		; REPLACE! 
      $_Count = $_Count - 1				; reduce the count 
    Else
      $StringReplace = $StringReplace + $_S1		; original text - no replacement 
    EndIf
    $_Min = 1						; skip the first element in the loop 
  EndIf
 
  For $_I = $_Min to $_Max
    $StringReplace = $StringReplace + $_aSource[$_I]	; add original text component 
    If $_I < $_Max And $_aSource[$_I]			; handle replacable last element 
      If $_I >= $_Start And $_Count			; see if we should replace based on start and count 
        $StringReplace = $StringReplace + $_S2		; REPLACE! 
        $_Count = $_Count - 1				; reduce count 
      Else
        $StringReplace = $StringReplace + $_S1		; original text, no replacement 
      EndIf
    EndIf
  Next
 
  $_Case = SetOption('CaseSensitivity', $_Case)		; restore original value 
 
  Exit 0
 
EndFunction