简体   繁体   English

从文本文件中写入文本以列出C#Windows Phone

[英]Write text from text file to list c# windows phone

i am beginer and i don't know how to write from text file to list some strings. 我是初学者,我不知道如何从文本文件中写出一些字符串。 I mean i have a file : 我的意思是我有一个文件:

A B
C D
E F
G H
...

and i want to write it to list but i dont know how, maybe it is simple but i tried something and it doesnt work. 而且我想将其写到列表中,但我不知道如何,也许这很简单,但是我尝试了一些方法,但没有用。 Now i have 我现在有

List<List<string>> listaKolumn = new List<List<string>>();
var str = Application.GetResourceStream(new Uri("wzor.txt", UriKind.Relative));
StreamReader sreader = new StreamReader(str.Stream);

int x = 0;
while (!sreader.EndOfStream)
{
foreach (string k in sreader.ReadToEnd().Split(new char[] { ' ' }))
            {
                int j = 0;
                foreach (string l in k.Split(new char[] {' '}))
                {
                    if (listaKolumn.Count < k.Split(new char[] { ' '}).Length)
                    {
                        listaKolumn.Add(new List<string>());
                    }
                    //double temp2;
                    listaKolumn[j].Add(l);
                    j++;
                }
            }
}

but something is wrong. 但是出了点问题。 i know how it should be only in mind but i dontn know language very well and i can't write it. 我知道应该只记住它,但是我不太了解语言,所以我不会写。

IN SHORT i need this text wite in to multidimensional array like array[0][0] = A array[0][1]=B array[1][0] =C array[1][1] = D and so on 总之,我需要将此文本写入多维数组,例如array [0] [0] = A array [0] [1] = B array [1] [0] = C array [1] [1] = D,依此类推上

Here is a method to pass in the text file you displayed in the question and return a multidimensional array. 这是一种传递您在问题中显示的文本文件并返回多维数组的方法。

public string[][] MultiArrayFromTextFile(string filePath)
{
    return File.ReadLines(filePath).Select(s => s.Split(' ')).ToArray();
}

File.ReadLines(filePath) returns a collection of all lines in the text file File.ReadLines(filePath)返回文本文件中所有行的集合

.Select is an extension method that takes a function. .Select是具有功能的扩展方法。

s=>s.Split(' ') is the function passed into .Select , which splits the string s by all spaces and returns an array of strings. s=>s.Split(' ')是传递给.Select的函数,该函数将字符串s s=>s.Split(' ')为所有空格并返回字符串数组。

.ToArray() takes the collection of string arrays created by .Select and makes an array out of that, so you get array of arrays. .ToArray()接受由.Select创建的字符串数组的集合,并.ToArray()创建一个数组,因此可以得到数组数组。

You can access the items in the results like this... 您可以像这样访问结果中的项目...

Console.WriteLine(results[1][1]); // returns 'D' ... so 2nd item of 2nd array

If I understand you right this should do the trick var listaKolumn = new List(); 如果我理解的正确,这应该可以解决问题var listaKolumn = new List();

        var sreader = new StreamReader(@"f:\wzor.txt");

        string line;
        while ((line = sreader.ReadLine())!=null)
        {
            listaKolumn.Add(line);
        }

There doesn't seem to be any reason for you to have a list of lists (List>) - that would give you to lists. 似乎没有任何理由让您拥有列表列表(List>)-这可以使您获得列表。

If you need to split on whitespaces you can do that in the while-loop, like 如果需要在空白处进行拆分,则可以在while循环中进行拆分,例如

foreach (var parts in line.Split(' '))
                {
                    listaKolumn.Add(parts);    
                }

Or by using LINQ 或使用LINQ

listaKolumn.AddRange(line.Split(' '));

Hope this helps... 希望这可以帮助...

Okay - then I didn't understand it right. 好的-那我听不懂。 Found a smarter way to read the lines into a collection. 找到了一种更聪明的方式来将行读入集合中。 This should get you pretty close at least :) 这至少应该使您非常接近:)

private static int LineCompare(string filepath1, string filepath2)
        {
            var result = 0;

            var list = File.ReadLines(filepath1).ToArray();
            var list2 = File.ReadLines(filepath2).ToArray();

            for (var i = 0; i < list.Length; i++)
            {
                if (list[i].Equals(list2[i]))
                    result++;
            }

            return result;
        }

Or the trimmed version: 或修剪的版本:

private static int LineCompare(string filepath1, string filepath2)
        {
            var list = File.ReadLines(filepath1).ToArray();
            var list2 = File.ReadLines(filepath2).ToArray();

            return list.Where((t, i) => t.Equals(list2[i])).Count();
        }

This of course compares lines with the same index, so if you need to compare line 5 in file1 with line 2 in file2. 当然,这将比较具有相同索引的行,因此,如果需要将file1中的第5行与file2中的第2行进行比较。 You would need some kind of loop. 您将需要某种循环。

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

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