How to concatenate a string array in auto hotkey

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-07 11:20:42

问题


Those who use, know, how useful automation tool AHK is..

The AHK has function StringSplit or StrSplit() which does very fast split string into array elements.

This is very useful if you want to manipulate some parts of well formed string, but unfortunately it appears there is no way around!

I spend time searching and there was a mess of samples with old syntax which just does not work. All I wanted is Final_Concatenated_String := StrConcat(My_Array_Of_Strings, "\") which obviously does not work!

So, simple question: how to concatenate simple array of strings?


回答1:


Spending lot of time, and finding old syntax examples which does not work made my do hard way, to make it simple.

Simple and fast solution for concatenating directory spliced into strings array:

Loop, % folder_path_array.MaxIndex()   ; concat string array
    {
        folder_path .= folder_path_array[A_Index]"\"
    }

More advanced version, in case you have ending backslash in path field:

Loop, % folder_path_array.MaxIndex()   ; concat array
    { if folder_path_array[A_Index]    ; if [last] element of array is empty, skip it 
        folder_path .= folder_path_array[A_Index]"\"
    }

In more deep details. I needed to copy directory path from input field, change the root directory, paste it back to input field and save it.

So I ended up with this script:

SendInput, ^a                ; select all input field text
SendInput, ^c                ; copy current selection to clipboard
ClipWait, 30

folder_path_array := StrSplit(Clipboard, "\")   ; split folder path into strings of array
folder_path_array[2] .= "_backup"           ; prepend string to root folder, first element is "C:"

    Loop, % folder_path_array.MaxIndex()   ; concat string array
        { if folder_path_array[A_Index]    ; if [last] element of array is empty, skip it 
            folder_path .= folder_path_array[A_Index]"\"
        }


Clipboard := folder_path        ; load the new string to clipboard
SendInput, ^v                   ; paste the new string into input field

Hope it will help somebody also.



来源:https://stackoverflow.com/questions/62608368/how-to-concatenate-a-string-array-in-auto-hotkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!