[英]I need to create an array that indexes each line of text as a new index. C#/Xamarin/Android
我有一個文本文件,其中有7000多行(以列表形式)。 我需要創建一個將每行更改為新索引的數組,以便可以在每個項目旁邊添加復選框。 (如果選中,它將打印到.txt文件。)
我找到了一些有關如何執行此操作的文章,但沒有一篇真正回答我的問題。 老實說,我什至不知道從哪里開始將其轉換為數組。 請讓我知道您需要什么信息來幫助我。
string filename = @"C:\Example\existingfile.txt";
var lines = new List<string>();
using (StreamReader reader = File.OpenText(filename))
{
while(!reader.EndOfStream) lines.Add(reader.ReadLine());
}
然后,您可以像數組一樣遍歷List
。
如果您需要通過讀取文件來創建List對象,並且希望將每一行都作為一個列表項,則可以執行以下操作。
//File name
const string f = "TextFile1.txt";
// Declare new List.
List<string> lines = new List<string>();
// Use using StreamReader for disposing.
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
lines.Add(line);
}
}
然后,如果您希望將此列表轉換為數組,則可以使用List對象的.ToArray()方法。
string[] arrayOfLines = lines.ToArray();
希望我能正確回答您的問題,這對您有所幫助。 謝謝。
如果您的列表存儲在字符串中,則可以通過以下方式嘗試:
String largeList;
String nl = System.getProperty("line.separator");
String[] lines = largeList.split(nl);
行現在包含行[x]-> x是行號可訪問的每一行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.