简体   繁体   English

逐行读取文本文档的所有内容

[英]Reading All contents of a text document line by line

I have a text document with multiple lines which contain a persons name, age, favourite number and their favourite activity. 我有一个包含多行的文本文档,其中包含人员姓名,年龄,喜欢的号码及其喜欢的活动。

For example: 例如:

Line 1: Josh 30 100 Likes to play soccer. 
Line 2: May 21 3 Likes to dance.
etc.

I have a message box which I would like to show their name, age, favourite number and activity one by one. 我有一个消息框,我想一一显示他们的姓名,年龄,喜欢的号码和活动。

How can I do this and how can I pick up their number as integers too? 我该怎么办?我也该如何将它们取为整数? thanks. 谢谢。

Sorry I have no code as I don't know what to do but I was thinking of using streamreader. 抱歉,我没有代码,因为我不知道该怎么做,但我当时正在考虑使用Streamreader。

Here are the tools you need for your task: 以下是完成任务所需的工具:

For reading the lines of the file (Blorgbeard's suggestion): 读取文件的各行 (Blorgbeard的建议):

string[] lines = File.ReadAllLines("file.txt");

For getting the values of each line (assuming that the names can be one-word only): 为了获取每一行的值 (假设名称只能是一个单词):

string[] values = line.Split(new char[]{' '},4);

For converting a value from String to Integer : 要将值从String转换为Integer

int intValue = Convert.ToInt32(strValue);

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

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