Jump to content

Multiple replace


Recommended Posts

Welcome. I'm Pole, and my english is weak so will use the google translator. I have a problem because I would like one command replace letters in the text "abti".
a = %charmap:0,1%
b =
%charmap:0,2%

t = %charmap:0,3%

i = %charmap:0,4%
the result would be %charmap:0,1%%charmap:0,2%%charmap:0,3%%charmap:0,4%

Edited by MrKris7100
Link to comment
Share on other sites

well, one 'command', that is actually a function :)...the function only replaces characters once, so you don't have to worry about it returning cbc, or aba...actual return will be cba

#include <array.au3>

Global Enum $giReplace_Find, $giReplace_Replace, $giReplace_UBound
Global $aReplace[2][$giReplace_UBound]=[["a","c"],["c","a"]]
Global $string = "abc"

$string = MultiReplace($string,$aReplace)
ConsoleWrite($string & @CRLF)

Func MultiReplace($sCallersString, $aCallersReplace)
    $aString = StringToASCIIArray($sCallersString)
    Local $aString2d[UBound($aString)][2], $sReturn = ""
    For $i = 0 to UBound($aString2d)-1
        $aString2d[$i][0] = Chr($aString[$i])
        $aString2d[$i][1] = False
    Next

    ; Do replaces on chars that have not already been replaced
    For $i = 0 to UBound($aString2d)-1
        If Not $aString2d[$i][1] Then
            For $j = 0 To UBound($aCallersReplace)-1
                If $aString2d[$i][0] = $aCallersReplace[$j][$giReplace_Find] Then
                    $aString2d[$i][0] = $aCallersReplace[$j][$giReplace_Replace]
                    $aString2d[$i][1] = True
                    ExitLoop
                EndIf
            Next
        EndIf
    Next

    ; Convert the array back to a string
    For $i = 0 to UBound($aString2d)-1
        $sReturn &= $aString2d[$i][0]
    Next

    Return $sReturn
EndFunc
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

changed the string on me, here ya go (not sure if those are actual chars, or literal strings...)  literal strings:

#include <array.au3>

Global Enum $giReplace_Find, $giReplace_Replace, $giReplace_UBound
;~ Global $aReplace[2][$giReplace_UBound]=[["a","c"],["c","a"]]
Global $aReplace[4][$giReplace_UBound]=[["a","%charmap:0,1%"],["b","%charmap:0,2%"],["t","%charmap:0,3%"],["i","%charmap:0,4%"]]

;~ Global $string = "abc"
Global $string = "abti"

$string = MultiReplace($string,$aReplace)
ConsoleWrite($string & @CRLF)

Func MultiReplace($sCallersString, $aCallersReplace)
    $aString = StringToASCIIArray($sCallersString)
    Local $aString2d[UBound($aString)][2], $sReturn = ""
    For $i = 0 to UBound($aString2d)-1
        $aString2d[$i][0] = Chr($aString[$i])
        $aString2d[$i][1] = False
    Next

    ; Do replaces on chars that have not already been replaced
    For $i = 0 to UBound($aString2d)-1
        If Not $aString2d[$i][1] Then
            For $j = 0 To UBound($aCallersReplace)-1
                If $aString2d[$i][0] = $aCallersReplace[$j][$giReplace_Find] Then
                    $aString2d[$i][0] = $aCallersReplace[$j][$giReplace_Replace]
                    $aString2d[$i][1] = True
                    ExitLoop
                EndIf
            Next
        EndIf
    Next

    ; Convert the array back to a string
    For $i = 0 to UBound($aString2d)-1
        $sReturn &= $aString2d[$i][0]
    Next

    Return $sReturn
EndFunc

Notice, that you just have to change the conversion array (line 5), to do any replaces...and any count of replaces

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...