Nicht angemeldet.
Hier können Sie Ihre eigenen Skripte und UDF's veröffentlichen undanderen zum Download anbieten oder von anderen herunterladen
; Example $hGUI = GUICreate("_StringReplaceEx", 400, 475) GUICtrlCreateLabel("", 19, 429, 202, 32) GUICtrlSetBkColor(-1, 0) GUICtrlCreateLabel("Hover your mouse over a control" & @CRLF & "to get further information.", 20, 430, 200, 30, 0x01) $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) WinSetTrans(GUICtrlGetHandle($edit), "", 100) $button = GUICtrlCreateButton(" _StringReplaceEx ", 250, 430) GUICtrlSetTip(-1, "Start Replacement") GUISetState() While True Switch GUIGetMsg() Case -3 Exit Case $button $newString = _StringReplaceEx(GUICtrlRead($edit), GUICtrlRead($Search), GUICtrlRead($Replace), GUICtrlRead($FirstOccurence), GUICtrlRead($HowManyTimes)) ConsoleWrite("@extended: " & @extended & @CRLF) If Not @error Then GUICtrlSetData($edit, $newString) Else Local $sErr Switch @error Case 1 $sErr = "Function called with invalid parameters" Case 2 $sErr = "Searchstring was not found at the given occurence" EndSwitch MsgBox(16, "Error!", "_StringReplaceEx returned @error." & @CRLF & "Error Code: " & @error & @CRLF & $sErr) EndIf EndSwitch WEnd ; UDF ;================================================================================================= ; 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, Abs($i) - 1, $sString) EndFunc ;==>_StringReplaceEx ;================================================================================================= ;#####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 ;==>__StringReplaceEx_Reverse ;=================================================================================================
Dauerhaft angemeldet bleiben?