问题
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