Search code examples
splitautoit

Unexpected behavior from _StringExplode()


I have a string: string1 string2 - string3 string4. I need it split in two at the - (note the space on either side of the "-"). I have the following code which isn't working as expected:

#include <MsgBoxConstants.au3>
#include <String.au3>

Local $test = _StringExplode("string1 string2 - string3 string4", " - ")

MsgBox($MB_SYSTEMMODAL, "Title", $test[1])

The output is string2. I expected it to be string3 string4.

enter image description here

Must be a small oversight but I'm having trouble finding it.


Solution

  • … explain what I'm doing wrong …

    It's a bug concerning AutoIt v3.3.12.0 (solved in successive beta). Alternatively StringSplit() can be used:

    #include <MsgBoxConstants.au3>
    #include <StringConstants.au3>
    #include <Array.au3>
    
    Global Const $g_aTest = StringSplit('string1 string2 - string3 string4', ' - ', $STR_ENTIRESPLIT)
    
    MsgBox($MB_SYSTEMMODAL, 'Title', $g_aTest[2])
    _ArrayDisplay($g_aTest)
    

    Including $STR_NOCOUNT to StringSplit()'s flag parameter returns array identical to _StringExplode().