Sensillium Posted August 6, 2014 Share Posted August 6, 2014 Hi, I've just started to use AutoIT after spending time learning Python & Powershell. I'm hoping there is a method for variable substitution in strings, without constant concatenation. For example: first = "a" second = "hope" third = "bar" foo = "This is %1 test, I %2 it will produce %3" "a", "hope", "bar" or foo = "This is %1 test, I %2 it will produce %3" first, second, thrid would produce: print foo This is a test, I hope it will produce bar or in Python if you prefer: foo = "This is {0} test, I {1} it will produce {2}".Format("a", "hope", "bar") or foo = "This is {0} test, I {1} it will produce {3}".Format(first, second, third) Currently I'm resorting to concatenation which is much more prone to typos, and tedious to write. e.g. (using AutoIt syntax) $foo = "This is " & $first & " test, I " & $second & " it will produce " & $third is this hope in vain, I've search the internet in general and these forums for: Substitution Variable substitution String substitution and get roughly the same 26 results for each, none of which are doing what I'm trying (most are regex), whereas searching for 'placehold' gets me 7 results about CD-ROM drives... Thank you for your assistance. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 6, 2014 Moderators Share Posted August 6, 2014 Sensillium,Soemthing like this perhaps? #include <MsgBoxConstants.au3> Local $aA[3] =["a", "hope", "bar"] MsgBox($MB_SYSTEMMODAL, "Concatenation", "This is " & $aA[0] & " test, I " & $aA[1] & " it will produce " & $aA[2])M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Solution Malkey Posted August 6, 2014 Solution Share Posted August 6, 2014 Using StringFormat function. Local $foo = StringFormat("This is %s test, I %s it will produce %s", "a", "hope", "bar") MsgBox(0, "Test1", $foo) ;or Local $first = "a" Local $second = "hope" Local $third = "bar" Local $sFoo = StringFormat("This is %s test, I %s it will produce %s", $first, $second, $third) MsgBox(0, "Test2", $sFoo) Link to comment Share on other sites More sharing options...
Sensillium Posted August 6, 2014 Author Share Posted August 6, 2014 (edited) Thanks for the suggestion Melba23, but aside from creating an array of the strings, instead of single variable names it doesn't actually change anything. As you probably noticed the Python line has only 1 pair of " for the entire string, whereas AutoIt has 3 pairs of ". I'm trying to replicate the single pair of " syntax. Edited August 6, 2014 by Sensillium Link to comment Share on other sites More sharing options...
Sensillium Posted August 6, 2014 Author Share Posted August 6, 2014 Perfect Malkey, thank you. Oddly I have read about StringFormat, but was unable to find a working example that matched my criteria. Link to comment Share on other sites More sharing options...
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