$hGUI = GUICreate("_StringReplaceEx",400,475) $edit = GUICtrlCreateEdit("I am a text, containing the world Hello."&@CRLF& _ "Hello used to be a very important word in the english language."&@CRLF& _ "Without 'Hello', nobody could greet another person."&@CRLF&@CRLF& _ "I am a text, containing the world Hello."&@CRLF& _ "Hello used to be a very important word in the english language."&@CRLF& _ "Without 'Hello', nobody could greet another person.",20,20,360,300) $Search = GUICtrlCreateInput("Hello",150,330,200,25) GUICtrlSetTip(-1,"The substring to search.") GUICtrlCreateLabel("Search:",70,335,-1,25) GUICtrlSetTip(-1,"The substring to search.") $Replace = GUICtrlCreateInput("Good morning",150,360,200,25) GUICtrlSetTip(-1,"The replacement string.") GUICtrlCreateLabel("Replace:",70,365,-1,25) GUICtrlSetTip(-1,"The replacement string.") GUICtrlCreateLabel("How Many Times:",20, 400) GUICtrlSetTip(-1,"The number of times to replace the searchstring."&@CRLF&"Set this to '0', if you want the function to replace all occurences of the searchstring,"&@CRLF&"starting with the determined occurence in $iOccurence"&@CRLF&"Use a negative value, to search from the right to left.") $HowManyTimes = GUICtrlCreateInput("2",125,395,50,25) GUICtrlSetTip(-1,"The number of times to replace the searchstring."&@CRLF&"Set this to '0', if you want the function to replace all occurences of the searchstring,"&@CRLF&"starting with the determined occurence in $iOccurence"&@CRLF&"Use a negative value, to search from the right to left.") GUICtrlCreateUpdown(-1) GUICtrlCreateLabel("First Occurence:",200, 400) GUICtrlSetTip(-1,"The substrings occurence to start with.") $FirstOccurence = GUICtrlCreateInput("3",300,395,50,25) GUICtrlSetTip(-1,"The substrings occurence to start with.") GUICtrlCreateUpdown(-1) $button = GUICtrlCreateButton(" _StringReplaceEx ",250,430) GUISetState() While True Switch GUIGetMsg() Case -3 Exit Case $button $newString = _StringReplaceEx(GUICtrlRead($edit),GUICtrlRead($Search),GUICtrlRead($Replace),GUICtrlRead($FirstOccurence),GUICtrlRead($HowManyTimes)) If Not @error Then GUICtrlSetData($edit,$newString) Else MsgBox(16,"Error!","_StringReplaceEx returned @error."&@CRLF&"Error Code: "&@error) EndIf EndSwitch WEnd ;================================================================================================= ; Function: _StringReplaceEx($sString,$sSearchString,$sReplaceString[,$iOccurence[,$iHowOften[,$iCaseSense]]]) ; Description: Extends StringReplace(). Giving you the ability to choose the first occurence to start with, ; and how many times the Searchstring should be replaced. ; ; Parameter(s): $sString - The string to evaluate. ; $sSearchString - The substring to search. ; $sReplaceString - The replacement string. ; $iOccurence - (optional) The substrings occurence to start with. (1) by default. ; $iHowOften - (optional) The number of times to replace the searchstring. ; (1) by default. Set this parameter to (0) if you want the ; function to replace all occurences of the Searchstring, starting with the one ; in $iOccurence. Use a negative value, to search from the right to left. ; $iCaseSense - (optional) Flag to indicate if the operations should be case sensitive. ; 0 = not case sensitive, using the user's locale (default) ; 1 = case sensitive ; 2 = not case sensitive, using a basic/faster comparison ; ; Requirement(s): Valid parameters :P ; ; Return Value(s): On Success - Returns the new string ; On Failure - Returns 0 ; @Error - 0 = No error. ; 1 = Function called with invalid parameters ; 2 = Searchstring was not found at the given occurence ; ; Author(s): SEuBo (www.autoit.de) ; Note(s): ;================================================================================================= Func _StringReplaceEx($sString,$sSearchString,$sReplaceString,$iOccurence=1,$iHowOften=1,$iCaseSense=0) If $sSearchString = "" Or $sString = "" Or $iOccurence < 1 OR $iCaseSense < 0 Or $iCaseSense > 2 Then Return SetError(1,0,0) Local $i = 1 If $iHowOften = 0 Then $i = -1 If $iHowOften >= 0 Then If Not StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence) Then Return SetError(2,0,0) Do $sString = StringLeft($sString,StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-1)&StringReplace(StringTrimLeft(StringTrimRight($sString,StringLen($sString)-StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-StringLen($sSearchString)),StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-1),$sSearchString,$sReplaceString)&StringRight($sString,StringLen($sString)-StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-StringLen($sSearchString)) If $iHowOften <> 0 Then $i += 1 Until $i > $iHowOften OR Not StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence) Else Local $i = -1 $sString = _StringReplaceEx_Reverse($sString) $sSearchString = _StringReplaceEx_Reverse($sSearchString) $sReplaceString = _StringReplaceEx_Reverse($sReplaceString) If Not StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence) Then Return SetError(2,0,0) Do $sString = StringLeft($sString,StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-1)&StringReplace(StringTrimLeft(StringTrimRight($sString,StringLen($sString)-StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-StringLen($sSearchString)),StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-1),$sSearchString,$sReplaceString)&StringRight($sString,StringLen($sString)-StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence)-StringLen($sSearchString)) $i -= 1 Until $i < $iHowOften OR Not StringInStr($sString,$sSearchString,$iCaseSense,$iOccurence) $sString = _StringReplaceEx_Reverse($sString) EndIf Return SetError(0,$i,$sString) EndFunc ;================================================================================================= ;#####INTERNAL USE##### ;_StringReplaceEx_Reverse : Reverses the contents of the specified string. (without a dll-call) ;SEuBo (www.autoit.de) ;================================================================================================= Func _StringReplaceEx_Reverse($sString) Local $sReverse = "" For $x = 1 to StringLen($sString) $sReverse &= StringRight($sString,1) $sString = StringTrimRight($sString,1) Next Return $sReverse EndFunc ;=================================================================================================