繁体   English   中英

如何在 C# 中拆分字符串并存储在数组中以及如何在特定字符串后添加新行

[英]How to Split Strings and Store in Array in C# and How to Add New Line After a Particular String

`我想读取info1.txt文件,想用这种方式写入另一个文本文件info2.txt

Id      Name     Address      DOB      Phone
1       abcd      efg      1/16/2021  987654323
2       hijkl     mno      2/16/2021  678987652

这是info1.txt

Id:1
Name:abcd
Address:efg
DOB:1/16/2021 3:31:22 PM
Phone:987654323

Id:2
Name:hijkl
Address:mno
DOB:2/16/2021 3:31:22 PM
Phone:678987652

而且info2.txt就像我提到的上面的表格格式,也想删除“ 3:31:22 PM”

我的代码


        static void Main(string[] args)
        {
            FileStream fsRead = new FileStream("E:\\Assignment\\info1.txt", FileMode.Open, FileAccess.Read);
            StreamReader srObj = new StreamReader(fsRead);
            FileStream fsWrite = new FileStream("E:\\Assignment\\info2.txt", FileMode.Create, FileAccess.Write);
            StreamWriter swObj = new StreamWriter(fsWrite);
           

            while (srObj.Peek() > 0)
            {
                string str;
                string[] strArray;
                
                str = srObj.ReadLine();
                str = str.Replace(" 3:31:22 PM", "");
                strArray = str.Split(':');

                
                    if (strArray.Length > 1)
                    {
                         swObj.Write(strArray[1]);
                         swObj.Write(" ");
                   
                     }
               }
swObj.Close();
                fsWrite.Close();
            srObj.Close();
            fsRead.Close();
           Console.ReadKey();
        }

我会将文件解析为字典列表,其中每个字典的键都是列。

首先将文件行拆分为字符串数组。 您可以为此使用File.ReadAllLines 然后将数组发送到解析行的 function。

public static List<Dictionary<string, string>> Parse(string [] lines)
{
    List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
    Dictionary<string, string> temp = new Dictionary<string, string>();
    foreach (var line in lines) {
        var parts = line.Split(new[] { ':' }, 2);
        if (parts.Length == 2) {
            temp[parts[0]] = parts[1];
        }
        else {
            if (temp.Count > 0) data.Add(temp);
            temp = new Dictionary<string, string>();
        }
    }
    if (temp.Count > 0) data.Add(temp);
    return data;
}

然后,制作一个 function 将列表写入文件。

public static void PrintTable(List<Dictionary<string, string>> users, TextWriter stream)
{
    if (users.Count == 0) return;
    
    // Print the header line
    foreach(var pair in users[0]) {
        stream.Write("{0,-12}", pair.Key);
    }
    stream.WriteLine();
    
    foreach (var user in users) {
        foreach(var pair in user) {
            // Special handling for DOB
            if (pair.Key == "DOB") stream.Write("{0,-12}", pair.Value.Split(' ')[0]);
            else stream.Write("{0,-12}", pair.Value);
        }
        stream.WriteLine();
    }
}

暂无
暂无

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

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