从文件读取数组 [英] Reading array from a file
本文介绍了从文件读取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个名为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 intoList<string>
s
- 使用
File.ReadAllText
方法读取文本文件的全部内容,该方法在 http://msdn中解释. microsoft.com/en-us/library/ms143368.aspx [ ^ ]转换为字符串 - 使用
Regex.Split
方法(模式为@"}\s*{"
- string 拆分为主数组字符串数组>编写一个以
string
作为参数并返回List<string>
- 的方法,在该方法内部使用
Regex.Matches
方法,其模式为@"{([^{}]*)}"
,该模式将{ }
中的元素作为组捕获元素 - 创建一个
List<string>
,将每个match.Groups[1].Value
添加到此列表中,然后返回此构造的列表 - 将在步骤2中获得的每个
string
传递给此方法,以获得每个内部列表
- 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 - Split this
string
into an array of main array strings usingRegex.Split
method with the pattern@"}\s*{"
- Write a method which takes a
string
as parameter and returns aList<string>
- Inside the method use
Regex.Matches
method with the pattern@"{([^{}]*)}"
, which captures the elements within{ }
as group elements - Create a
List<string>
, add eachmatch.Groups[1].Value
to this list and return this constructed List - Pass each
string
obtained in step 2 to this method to obtain each inner list
这篇关于从文件读取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文