在C#中处理多行文本

时间:2018-03-29 08:58:06

标签: c# multiline

我正在尝试使用不寻常的模式处理一些文本。文字如下所示:

  

||姓名:XYZ DJ年龄:23​​岁
位置:纽约;端;'
  2018-03-20 11:59:59.397,mnx = 0x0000700,pid = 90c9ac,xSG:dlgID:34   AppDlg:774 params:
2018-03-20 11:59:59.397,mnx = 0x700000,   pid = 090c9ac,lBG:OPCDManager ::响应:0x7f083
2018-03-20   11:59:59.397,mxn = 0x000070,pid = f90c9ac,lBG:DlgID:37774 sess:' 990'   conID:1 dlClose:false params:

现在,我想将此数据加载到文本文件中,如下所示:

  

XYZ DJ-23岁 - 纽约,2018-03-20 11:59:59.397,mnx = 0x0000700,   pid = 90c9ac,xSG:dlgID:34 AppDlg:774 params:
XYZ DJ-23 Years-New   York,2018-03-20 11:59:59.397,mnx = 0x700000,pid = 090c9ac,   lBG:OPCDManager ::响应:0x7f083
XYZ DJ-23岁 - 全新   York,2018-03-20 11:59:59.397,mxn = 0x000070,pid = f90c9ac,   lBG:DlgID:37774 sess:' 990' conID:1 dlClose:false params:

我已经尝试过以下代码,但它没有给我我想要的东西。相反,它给了我一个长文本字符串而不是几行:

string linesc = File.ReadAllText(path);
string[] linesx = linesc.Split('|');
foreach (string s in linesx)
{
    string new2=s.Replace(Environment.NewLine, " ");
    File.AppendAllText(path2 + "myfile.txt", new2 + Environment.NewLine);
}

如何修改代码以便获取上面的行?

3 个答案:

答案 0 :(得分:0)

你必须将其分解为几个步骤:

  1. 从前三行中提取“全局”信息
  2. 遍历列表行并将它们连接到输出格式
  3. 此刻你正在分裂'|'这导致["", "", "Names : XYZ ... your whole other text"]

    例如(未经测试):

    string[] linesc = File.ReadAllText(path).Split(new string[]{Environment.NewLine}, StringSplitOptions.None);
    // extract global infos
    string[] name = linesc[0].Split(':');
    string[] age = linesc[1].Split(':');
    string[] location = linesc[2].Split(':');
    
    for (int i=3; i<linesc.Length; i++)
    {
        // reconcatenate new line
        string new2 = name[1] + "-" + age[1] + "-" + location[1] + "," + linesc[i];
        File.AppendAllText(path2 + "myfile.txt", new2 + Environment.NewLine);
    }
    

    如果您还想摆脱前导/尾随空格,可以在每个字符串部分使用Trim()

答案 1 :(得分:0)

尝试以下方法:

string linesc = File.ReadAllText(path);
string[] linesx = linesc.Split('|');
foreach (string s in linesx)
{
    string new2=s.Replace(Environment.NewLine, " ")
        .Replace("Names : ", "")
        .Replace("Age : ", "")
        .Replace("Location : ", "") + "\n";
    File.AppendAllText(path2 + "myfile.txt", new2 + Environment.NewLine);
}

我还负责从输入中删除“姓名:”,“年龄:”和“位置:”。

答案 2 :(得分:0)

您也可以尝试这种方法:

        string text = @"||Names : XYZ DJ
                         Age : 23 Years
                         Location: New York; end;'
                         2018-03-20 11:59:59.397, mnx=0x0000700, pid=90c9ac, xSG: dlgID:34 AppDlg:774 params:
                         2018-03-20 11:59:59.397, mnx=0x700000, pid=090c9ac, lBG: OPCDManager::Response: 0x7f083
                         2018-03-20 11:59:59.397, mxn=0x000070, pid=f90c9ac, lBG: DlgID:37774 sess:'990' conID:1 dlClose:false params:";

        StringBuilder result = new StringBuilder();

        string[] allLines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        string name = allLines[0].Replace("||Names : ", string.Empty).Trim();
        string age = allLines[1].Replace("Age : ", string.Empty).Replace("Years", string.Empty).Trim();
        string location = allLines[2].Replace("Location: ", string.Empty).Replace("; end;'", string.Empty).Trim();

        for(int i = 3; i < allLines.Length; i++)
        {
            result.AppendLine($"{name}-{location},{allLines[i].Trim()}");
        }

        string res = result.ToString();
        Console.WriteLine(res);