How can I access a string like an array in AutoIt? (I'm porting code from C++ to AutoIt)

StackOverflow https://stackoverflow.com/questions/4597006

  •  15-10-2019
  •  | 
  •  

Question

Ok, gah, syntax conversion issue here...How would I do this in AutoIt?

String theStr = "Here is a string";
String theNewStr = "";

for ( int theCount = 0; theCount < theStr.Size(); theCount++ )
{
theNewStr.Append(theStr[theCount]);
}

I am trying to access individual chars within a string in AutoIt and extract them. Thats's it. Thanks.

Was it helpful?

Solution

What about this:

$theStr = StringSplit("Here is a string", "") ; Create an array
$theNewStr = ""

For $i = 1 to $theStr[0] Step 1
    $theNewStr = $theNewStr & $theStr[$i]
Next
MsgBox(0, "Result", $theNewStr)

OTHER TIPS

#include <string>
std::string theStr = "Here is a string";
std::string theNewStr; 
//don't need to assign blank string, already blank on create

for (size_t theCount = 0; theCount < theStr.Size(); theCount++ )
{
    theNewStr += theStr[theCount];
}
//or you could just do 
//theNewStr=theStr;
//instead of all the above

in autoit, it's just as simple to copy a string. to access a piece of a string (including a character, which is still a string) you use StringMid() which is a holdover from Microsoft BASIC-80 and now Visual BASIC (and all BASICs). you can stil do

theNewStr = theStr

or you can do it the hard way:

For $theCount = 1 to StringLen($theStr)
    theNewStr &= StringMid($theStr, $theCount, 1)
Next
;Arrays and strings are 1-based (well arrays some of the time unfortunately).

& is concatenation in autoit. stringmid extracts a chunk of a string. it MIGHT also allow you to do the reverse: replace a chunk of a string with something else. but I would do unit testing with that. I think that works in BASIC, but not sure about autoit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top