从文件读取数组 [英] Reading array from a file

查看:224
本文介绍了从文件读取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个名为test.txt
的文件 像这样的数组具有亚arras

{{first},{last},{age}}
{{first1},{last1},{age1}}

我试图将其从文件中读取到程序中的数组或列表中.
任何示例代码或如何执行?
非常感谢您的帮助

Hello Guys,
I have a file named test.txt
that has arrays with sub arrasy like this

{{first}, {last}, {age}}
{{first1}, {last1}, {age1}}

I am trying to read that from a file into array or list in my program.
any sample code or how to do it??
thanks alot for your help

推荐答案

以下步骤可用于将文本文件中的数据读取到List<string> s
The following steps can be used to read the data from the text file into List<string>s

  1. 使用File.ReadAllText方法读取文本文件的全部内容,该方法在 http://msdn中解释. microsoft.com/en-us/library/ms143368.aspx [ ^ ]转换为字符串
  2. 使用Regex.Split方法(模式为@"}\s*{"
  3. string 拆分为主数组字符串数组>编写一个以string 作为参数并返回List<string>
  4. 的方法,在该方法内部使用Regex.Matches方法,其模式为@"{([^{}]*)}",该模式将{ }中的元素作为组捕获元素
  5. 创建一个List<string>,将每个match.Groups[1].Value添加到此列表中,然后返回此构造的列表
  6. 将在步骤2中获得的每个string 传递给此方法,以获得每个内部列表

  1. Read the entire content of the text file using File.ReadAllText method explained here http://msdn.microsoft.com/en-us/library/ms143368.aspx[^] into a string
  2. Split this string into an array of main array strings using Regex.Split method with the pattern @"}\s*{"
  3. Write a method which takes a string as parameter and returns a List<string>
  4. Inside the method use Regex.Matches method with the pattern @"{([^{}]*)}", which captures the elements within { } as group elements
  5. Create a List<string>, add each match.Groups[1].Value to this list and return this constructed List
  6. Pass each string obtained in step 2 to this method to obtain each inner list


这篇关于从文件读取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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