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

本文关键字:两个 包含不 分隔 数组 文本 C#- 文件 读取 Visual | 更新日期: 2023-09-27 18:25:04

我正试图弄清楚如何读取文本文件,并将其值分离到一个包含所有字符串的数组和一个包含全部数字的数组中。我试过在谷歌上搜索,但什么也找不到。这是我的档案。。。

使命召唤:高级Warfare@17452.78
蝙蝠侠阿卡姆Knight@0.0
Carmageddon2015@734562.68
Halo UltimateCollection@45629.45
战争装备:黄金Edition@734562.56
我的小马-僵尸Version@452749.21
速度需求V=452893.21
国际足联2016@34981.45
蝙蝠侠阿卡姆Asylum@547892.45
NBA 2016=45274.89
黑暗的Sector@54378.23
狙击手埃利特3@63478.21
最后一位监护人=5233907.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");
        }
    }

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

你所做的一切只会给你介于"@"answers"="符号之间的一切。

您需要使用Regex.Split之类的方法来拆分数字或字母字符,并找到删除"@"answers"="字符的方法。首先,这里有一个简单的例子,说明您希望使用任意数量的一个或多个非数字字符作为分隔符。显然,为了你自己的目的,你需要更多:

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

你可以用一种巧妙的方式。。。

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调用。但如果你喜欢使用最少的线条,你可以保持这种方式。

查看您的代码,您只读取了一行文本。您有一个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");
    }
}