问题
I have a series of tab-delimited strings copied to the Windows Clipboard. I want to split these strings into arrays using the tab character.
Unit Dept_ID Name
CORP 0368 Admin
CORP 3945 Programmer
SESHAN 4596 Software Engineer
I'm trying to use StringSplit(), but I can't figure out how to use 'tab' as my delimiter. I've tried a few different methods, but none seem to work.
clipboard = %clipboard% ; Convert to plain text
StringSplit, MyArray, clipboard, `\t` ; Split string using tabs
MsgBox % "MyArray[1] = " . MyArray[1] ; BUG: Prints empty string
How can I split a tab-delimited string in AutoHotkey?
回答1:
First you need to split them to an array of lines with:
lines := StrSplit(clipboard, "`n")
Then you can loop over all the lines and split them to columns creating a multidimensional array:
columns := []
for index, value in lines
columns.Insert(StrSplit(value, "`t"))
; examples
MsgBox % columns[1][2] ; Dept_ID
MsgBox % columns[2][1] ; CORP
MsgBox % columns[2][2] ; 0368
Note that Autohotkey has 2 types of arrays the "new" type which are actually objects and you use them with arr[index]
and the older pseudo arrays. In your code you mixed them up, StringSplit returns a pseudo array and can't be used with []
. I recommend you read about arrays in the documentation.
回答2:
This splits tab delimited clipboard contents into an array
MyArray := StrSplit( clipboard, "`t" )
MsgBox % "MyArray[1] = " . MyArray[1]
Functionally equivalent
StringSplit MyArray, clipboard, `t
MsgBox MyArray1 = %MyArray1%
来源:https://stackoverflow.com/questions/45620437/how-can-i-split-tab-delimited-strings-in-autohotkey