簡體   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