2017-08-10 211 views
0

我有一系列製表符分隔的字符串複製到Windows剪貼板。我想要使​​用製表符將這些字符串拆分爲數組。如何在Autohotkey中分隔製表符分隔的字符串?

Unit Dept_ID Name 
CORP 0368 Admin 
CORP 3945 Programmer 
SESHAN 4596 Software Engineer 

我試圖使用StringSplit(),但我無法弄清楚如何使用「標籤」作爲我的分隔符。我嘗試了幾種不同的方法,但似乎沒有任何工作。

clipboard = %clipboard% ; Convert to plain text 
StringSplit, MyArray, clipboard, `\t` ; Split string using tabs 
MsgBox % "MyArray[1] = " . MyArray[1] ; BUG: Prints empty string 

我怎麼可以拆分在AutoHotkey的製表符分隔字符串?

回答

1

首先,你需要將它們拆分線的陣列:

lines := StrSplit(clipboard, "`n") 

然後你可以遍歷所有的行,並將它們拆分列創建多維數組:

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 

注意AutoHotkey的有2種類型的數組「新」型這實際上是對象,並使用他們arr[index]和舊的僞陣列。在你的代碼中,你將它們混合起來,StringSplit返回一個僞數組,並且不能與[]一起使用。我建議你閱讀關於documentation中的數組。

+0

根據你的回答,[我創建了一個可重用的函數,可以分割任意分隔的字符串。](https://pastebin.com/9RA5KpVH) –

1

這種分裂製表符分隔剪貼板中的內容到一個數組

MyArray := StrSplit(clipboard, "`t") 
MsgBox % "MyArray[1] = " . MyArray[1] 

其功能相當於

StringSplit MyArray, clipboard, `t 
MsgBox MyArray1 = %MyArray1%