繁体   English   中英

如何在文本文件中拆分逗号分隔的字符串并将每个字符串保存在不同的变量中?

[英]How do I split the comma separated string in my text file and save each string in different variable?

这些是我txt文件的内容,该文件保存在我的PC的D驱动器中

abc,1,2,3

我尝试了流动,但是没有用:

Dim stats() As String = File.ReadAllLines("C:/myfile.txt ")

Dim data() As String = line.Split(","c)
Dim query = From line In stats

Country = Data(0)

States = CInt(data(1))

noOfcoders = CInt(Data(2))

rank= CInt(Data(3))

holidays = CInt(Data(4))
contribution = CInt(Data(5))

编辑

我在这行上收到错误消息。data = line.Split(“,” c)...它说这行不再使用或有什么用,我宁愿使用LineInput,但那要求输入文件号而我不这样做不知道那是什么。 抱歉,我是一个完整的初学者。 我还能尝试什么?

我得到的确切错误:-

错误1'行'语句不再受支持。 文件I / O功能可作为“ Microsoft.VisualBasic.FileSystem.LineInput”使用,而图形功能可作为“ System.Drawing.Graphics.DrawLine”使用。 F:\\ My Documents \\ Mark Sanchez \\使用实验室编程(VB.net)\\ CIS170A_Lab07 \\ CIS170A_Lab07 \\ Form2.vb 62 16 CIS170A_Lab07


好的,该错误现在消失了,但是现在如果我想将这些值添加到列表框中..我该怎么做?

问题是您正在将文本文件逐行读取到字符串数组中。

stats不是字符串,而是字符串数组。 数组中的每个项目都是一个字符串,代表文本文件中的一行。

由于文件中只有一行,因此数组中只有一个条目。

Dim stats() As String = IO.File.ReadAllLines("C:/myfile.txt ")
Dim firstLine as String = stats(0)
Dim data() As String = firstLine.Split(","c)

Country = Data(0)
States = CInt(data(1))
noOfcoders = CInt(Data(2))
rank = CInt(Data(3))
holidays = CInt(Data(4))
contribution = CInt(Data(5))

@GrandMasterFlush也是正确的:您的代码引用了6个字段,但是您的文本文件片段仅包含4个值,因此此代码仍将失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM