如何在Autohotkey中分割制表符分隔的字符串? [英] How can I split tab-delimited strings in Autohotkey?
问题描述
我将一系列制表符分隔的字符串复制到Windows剪贴板.我想使用制表符将这些字符串分成数组.
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
我正在尝试使用 StringSplit(),但我不能弄清楚如何使用制表符"作为我的定界符.我尝试了几种不同的方法,但是似乎都没有用.
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
如何在AutoHotkey中拆分制表符分隔的字符串?
推荐答案
首先,您需要使用以下命令将它们拆分为行数组:
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
请注意,Autohotkey有2种类型的数组,即新"类型,它们实际上是对象,您可以将它们与arr[index]
和较旧的伪数组一起使用.在您混合的代码中,StringSplit返回一个伪数组,不能与[]
一起使用.我建议您在文档中阅读有关数组的信息.
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.
这篇关于如何在Autohotkey中分割制表符分隔的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!