繁体   English   中英

Visual C# - 从文本文件中读取并在包含不同值的两个数组中分隔值

[英]Visual C# - Reading from a text file and separating values in two arrays that hold different values

我试图弄清楚如何读取我的文本文件并将其值分隔到一个包含所有字符串的数组和一个包含所有数字的数组中。 我试过在谷歌上搜索,但找不到任何东西。 这是我的文件中的内容...

使命召唤:Advanced Warfare@17452.78
蝙蝠侠阿甘骑士@0.0
Carmageddon 2015@734562.68
Halo Ultimate Collection@45629.45
战争机器:黄金版@734562.56
我的小马驹 - 僵尸版@452749.21
极品飞车 V=452893.21
FIFA 2016@34981.45
蝙蝠侠阿卡姆疯人院@547892.45
NBA 2016=45274.89
暗界@54378.23
狙击精英 3@63478.21
最后的守护者=523907.21
巫师 3:狂猎=45294.34
圣徒行 4@53783.55
真人快打 X@423894.54

这是我的代码...

private void ReadIntoArray()
    {
        try
        {

            const int SIZE = 16;
            string[] titleArray = new string[SIZE];
            //double[] salesArray = new double[SIZE];
            int index = 0;
            StreamReader inputFile = File.OpenText("GameSales.txt");
            string title = inputFile.ReadLine();
            titleArray = title.Split('@', '=');


            while (index < titleArray.Length && !inputFile.EndOfStream)
            {                                      
                index++;
            }


            foreach (string value in titleArray)
            {
                detailsListBox.Items.Add(value);
            }

            inputFile.Close();

        }



        catch
        {
            MessageBox.Show("Error");
        }
    }

你在做什么只会给你 '@' 和 '=' 符号之间的一切。

您需要使用 Regex.Split 之类的东西来拆分数字或字母字符,并找到一种方法来删除 '@' 和 '=' 字符。 为了让您开始,这里有一个简单的示例,说明您想使用任意数量的一个或多个非数字字符作为分隔符。 显然,出于您自己的目的,您需要的不仅仅是这些:

// Split on one or more non-digit characters.
    string[] numbers = Regex.Split(input, @"\D+");

查看您的代码,您只阅读了一行文本。 您有一个什么都不做的 while 语句,您需要将文件处理移到该语句中。 您的 split 语句返回一组值,第一个是标题,第二个是您未使用它们的销售信息。 这个例子应该做你想要的。

private void ReadIntoArray()
{
    try
    {

        const int SIZE = 16;
        string[] titleArray = new string[SIZE];
        double[] salesArray = new double[SIZE];
        int index = 0;
        StreamReader inputFile = File.OpenText("GameSales.txt");


        while (index < titleArray.Length && !inputFile.EndOfStream)
        {
            string title = inputFile.ReadLine(); //Note the moving of the file readline into the while statement
            string[] temp = title.Split('@', '='); //Also used a temporary variable to hold the value of the split before assigning it to the array.
            titleArray[index] = temp[0];
            salesArray[index] = double.Parse(temp[1]);
            index++;
        }


        foreach (string value in titleArray)
        {
            detailsListBox.Items.Add(value);
        }
        inputFile.Close();

    }
    catch
    {
        MessageBox.Show("Error");
    }
}

你可以以一种巧妙的方式做到这一点......

public class Movie
{
    public string Series { get; set; }
    public string Name { get; set; }
    public decimal Value { get; set; }
}

然后在您的方法中执行此操作以填充您的电影列表。

    var allLines = File.ReadAllLines(@"C:\myfile.txt");
    List<Movie> movies =  new List<Movie>();
    movies.AddRange(allLines.Select(m=> new Movie()
    {
        Series = m.Split(new []{'@'})[0].Split(new []{'='})[0].Split(new []{':'})[0], 
        Name = (m.Split(new[] { ':' }).Length > 1 ? m.Split(new[] { ':' })[1] : m.Split(new[] { ':' })[0]).Split(new[] { '@' })[0].Split(new[] { '=' })[0], 
        Value = decimal.Parse(m.Contains("@")?m.Split(new []{'@'})[1]:m.Split(new []{'='})[1]),
    }));

您可以简化split调用。 但是,如果您更喜欢使用最少的行来完成此操作,则可以保持这种方式。

暂无
暂无

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

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