Spider001 Posted January 4, 2015 Posted January 4, 2015 (edited) Happy new year is this the best way to replace string from array ? #include <array.au3> Local $change[0] _ArrayAdd($change, "is|test|good|it") $text = "this %0 a %1 , and %3 works %2" ConsoleWrite($text & @CRLF) For $i = 0 To UBound($change) -1 $text = StringReplace($text,'%' & $i,$change[$i]) Next ConsoleWrite($text & @CRLF) Edited January 4, 2015 by Spider001
jguinch Posted January 4, 2015 Posted January 4, 2015 (edited) another way : #include <array.au3> Local $change[0] _ArrayAdd($change, "is|test|good|it") $text = "this %0 a %1, and %3 works %2" $text = Execute ( StringRegExpReplace('"' & StringReplace( $text , '"', '""') & '"', "%(\d+)", """ & Execute('\$change[$1]') & """) ) ConsoleWrite($text & @CRLF) Edit : Also, if you store each word in string variable, you can use AutoItSetOption("ExpandVarStrings", 1), like this : AutoItSetOption("ExpandVarStrings", 1) $var0 = "is" $var1 = "test" $var2 = "good" $var3 = "it" $text = "this $var0$ a $var1$ , and $var3$ works $var2$" ConsoleWrite($text) Maybe it's possible to do the same thing with an array (I don't know how...) Edited January 4, 2015 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Malkey Posted January 4, 2015 Posted January 4, 2015 I wouldn't say this example is a better way to replace a string from an array, just another way. $aChange = StringSplit("is|test|well|it", "|", 2) Local $text = "This %0 a %1, and %3 works %2." ConsoleWrite("Original: " & $text & @CRLF) Local $sTextChanged = Execute('"' & StringRegExpReplace($text, "%(\d+)", """ & $aChange[$1] & """) & '"') ConsoleWrite("Changed: " & $sTextChanged & @LF) #cs Returns:- Original: This %0 a %1, and %3 works %2. Changed: This is a test, and it works well. #ce
Moderators SmOke_N Posted January 5, 2015 Moderators Posted January 5, 2015 Malkey, your example really explains how the Execute() function works... Neat . Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
jguinch Posted January 5, 2015 Posted January 5, 2015 I don't know why I used Execute twice in my example... BTW, you must double the quotes to allow quotes in the original string, otherwise the replacement will fail : Local $change[4] = ["is", "test", "good", "it"] Local $text = "this %0 a ""%1"", and %3 works %2" ConsoleWrite ( Execute ( StringRegExpReplace('"' & $text & '"', "%(\d+)", """ & $change[$1] & """) ) & @CRLF) ; Doubling the quotes ConsoleWrite ( Execute ( StringRegExpReplace('"' & StringReplace( $text , '"', '""') & '"', "%(\d+)", """ & $change[$1] & """) ) & @CRLF) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now