来自文本文件的Visual Basic解析代码 [英] Visual Basic Parsing code from text file

查看:106
本文介绍了来自文本文件的Visual Basic解析代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!

我需要帮助来了解如何编写代码以解析.txt文件中的数据(如下)并将其存储为变量

83岁的小金(Kim)
76岁的杰克(Jake),
98岁的艾琳(Erin)
69岁的罗纳德(Ronald)
83岁的吉姆(Freshman)
凯特(91岁大二)
史蒂夫,64岁,新鲜人
89岁的朱莉(Julie)
75岁的Mark,高级
保罗,81岁,高级
56岁的麦克(Freshman)
97岁的梅根,少年
杰森,67岁,大二
玲88岁的老人
70岁的萝莉(Lori)

我相信我必须使用其中一些代码吗?

Hey!

I need help understanding how to write the code to parse this data (below) from a .txt file and store them as variables

Kim,83,Junior
Jake,76,Senior
Erin,98,Senior
Ronald,69,Senior
Jim,83,Freshman
Kate,91,Sophomore
Steve,64,Freshman
Julie,89,Junior
Mark,75,Senior
Paul,81,Senior
Mike,56,Freshman
Meghan,97,Junior
Jason,67,Sophomore
Ling,88,Senior
Lori,70,Freshman

I believe that I have to use some of this code?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As IO.StreamReader = IO.File.OpenText("input.txt")
        Dim strinput As String



但不知道还能做什么.谢谢!

[edit]添加了代码块[/edit]



but am not sure what else to do. Thank you!

[edit]code block added[/edit]

推荐答案

您的课程材料中对此进行了介绍.

您阅读了文件的每一行
在,"上分割行
根据需要处理数据
重复直到没有行为止.

那是您将获得的最好的.没人会为您写作业.
This is covered in your class materials.

You read each line of the file
split the line on ","
process the data as necessary
repeat until there are no more lines.

That''s the best you''re going to get. Nobody here is going to write your homework for you.


最好使用类System.IO.StreamReader:http://msdn.microsoft.com/zh-cn/library/system.io.streamreader.aspx [ http://msdn.microsoft.com/en-us/library/system.string. split.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system.int32. parse.aspx [ ^ ],
http://msdn.microsoft.com/zh-我们/library/system.uint32.parse%28v=vs.71%29.aspx [ http://msdn.microsoft.com/en-us/library/system.enum. parse.aspx [ ^ ].

对于每一行,请使用已解析的数据创建您的结构或类的实例,然后将其添加到集合中.

下次,尝试自己解决这些简单的问题,了解如何在MSDN帮助页面中找到所需的全部信息.

—SA
Better use the class System.IO.StreamReader: http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^].

Define enumeration type like enum Stage = { Freshman, Junior, Senior, ... }, than create a structure or a class with three fields and constructor with three parameters: string name, integer… I don''t know what, whatever 83, 76, 98, etc. mean, and Stage. Then, declare some collection with the items of this type, for example, System.Collections.Generic<>.

Read the file line by line, split each time in three strings using string.Split by '','', and parse integer value using int.Parse or int.TryParse (or uint.Parse, uint.TryParse) and enum using enum.Parse:
http://msdn.microsoft.com/en-us/library/system.string.split.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx[^],
http://msdn.microsoft.com/en-us/library/system.uint32.parse%28v=vs.71%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx[^].

For each line, create an instance of your structure or class out of parsed data and add to the collection.

Next time, try to solve such simple problems by yourself, learn how to find in MSDN help pages all you need.

—SA


这篇关于来自文本文件的Visual Basic解析代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆