如果第一个元素始终相同,则解析文本文件连接字符串

Parsing a text file concatenate a string if the first element is always the same

我有一个这种格式的文件:

**000002650**,00254,001,
**000002650**,02000,001,
**000002650**,02001,001,
000003781,00000,001,
000007790,00245,001,
000007790,02000,001,
000007790,02001,001,
000007907,00245,001,
000007907,02000,001,
000007907,02001,001,
000007998,00000,001,

我必须得到这个文件格式

HI:**2650**,254,2000,2001
HI:3781
HI:7790,245,2000,2001

所以当第一个元素等于下一行的下一个第一个元素时,我必须连接所有不同的元素

我可以在 C# 中做什么?

我现在的代码

using (StreamReader reader = new StreamReader(path))
                    {
                                                string sFile = string.Empty;
                        sFile = reader.ReadToEnd();


                        string[] asLine = sFile.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);


                        StringBuilder stringBuilder = new StringBuilder();

                        foreach (string sLine in asLine)
                        {

                            string[] asSegments = sLine.Split(',');

                            firstTemp = asSegments[1];

                            if (firstTemp == asSegments[1])
                            {
                                stringBuilder.Append()
                            }


                        } 

                    } 
        string[] lines = File.ReadAllLines(path);
        Dictionary<string, List<string>> collection = new Dictionary<string, List<string>>();
        foreach (var line in lines)
        {
            string[] tokens = line.Split(',');
            if (tokens.Length > 1)
            {
                if (collection.ContainsKey(tokens[0]))
                {
                    collection[tokens[0]].Add(tokens[1]);
                }
                else
                {
                    collection.Add(tokens[0], new List<string> { tokens[1] });
                }
            }
        }

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication47
{
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test.txt";
        const string OUTPUT_FILENAME = @"c:\temp\test1.txt";
        static void Main(string[] args)
        {
            Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
            using (StreamReader reader = new StreamReader(INPUT_FILENAME))
            {
                string input = "";
                string strKey = "";
                while ((input = reader.ReadLine()) != null)
                {
                    string[] inputArray = input.Split(new char[] { ',' });
                    if (inputArray[0].Contains("*"))
                    {
                        int key = int.Parse(inputArray[0].Replace("*", ""));
                        strKey = "**" + key + "**";

                    }
                    else
                    {
                        strKey = int.Parse(inputArray[0]).ToString();
                    }
                    if (dict.ContainsKey(strKey))
                    {
                        dict[strKey].Add(inputArray[1]);
                    }
                    else
                    {
                        dict.Add(strKey, new List<string>() { inputArray[1] });
                    }
                }
            }
            using (StreamWriter writer = new StreamWriter(OUTPUT_FILENAME))
            {
                foreach(KeyValuePair<string,List<string>> row in dict)
                {
                   writer.WriteLine("HI:{0},{1}", row.Key, string.Join(",", row.Value));
                }
            }

        }
    }

}