_StringReplaceEx()

  • Hallo,
    Ich habe die Funktion StringReplace mal ein bisschen erweitert. Möchte mal zum Beispiel in diesem Text hier:

    Spoiler anzeigen
    Zitat

    I am a text, containing the world Hello
    Hello used to be a very important word in the english language.
    Without 'Hello', nobody could greet another person

    I am a text, containing the world Hello
    Hello used to be a very important word in the english language.
    Without 'Hello', nobody could greet another person.

    Erst beim zweiten "Hello" mit dem Suchen und ersetzen starten, und beispielsweise nur 3 mal ersetzen lassen,
    so eigent sich _StringReplaceEx() wunderbar.

    Es werden natürlich keine weiteren includes benötigt. Ich habe absichtlich auf RegEx verzichtet,
    da man hiermit auch Binärdaten suchen und ersetzen können soll (Wers braucht :D)
    Halt ein waschechter Autoit-String-Befehl, der sich durch (fast) nichts aus der Fassung bringen lässt
    *hust* Unnötige Werbung *hust

    Beispiel ist dabei.

    Spoiler anzeigen
    [autoit]


    $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)

    [/autoit] [autoit][/autoit] [autoit]

    $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.")

    [/autoit] [autoit][/autoit] [autoit]

    $Replace = GUICtrlCreateInput("Good morning",150,360,200,25)
    GUICtrlSetTip(-1,"The replacement string.")
    GUICtrlCreateLabel("Replace:",70,365,-1,25)
    GUICtrlSetTip(-1,"The replacement string.")

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    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)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    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)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;=================================================================================================
    ; 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 (http://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)

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    ;=================================================================================================
    ;#####INTERNAL USE#####
    ;_StringReplaceEx_Reverse : Reverses the contents of the specified string. (without a dll-call)
    ;SEuBo (http://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
    ;=================================================================================================

    [/autoit] [autoit][/autoit] [autoit][/autoit]


    /EDIT (22.12.2009): Bug gefixed, der enstanden ist, wenn man $iHowOften auf 0 setzen wollte.

    Alte Downloads: 3