C#解析和数组[重复项]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

我正在编写一个解析某些数据的程序,但由于某种原因我没有收到我需要的东西。

我按以下顺序在文件中存储数据:

1111
username
[email protected]
IMAGE01: http://www.1234567890.net/image/cc_141019050341.png

因此,我创建了一个名为“ lines”的数组,文件中的每一行文本都有一个数据,然后:

    this.videoId = lines[0];
    this.clientUser = lines[1];
    this.clientEmail = lines[2];

    this.textLines = new List<string>();
    this.imageLines = new Dictionary<int,string>();

    for (int i = 3; i < lines.Length; i++)
    {
        if (lines[i].Contains("IMAGE"))
        {
            int imgNumber = Int32.Parse(
                    lines[i].Substring(Math.Max(0, lines[i].Length - 10), 2)
                );
            this.imageLines.Add(imgNumber, lines[i].Substring(Math.Max(0, lines[i].Length - 7)));
        }
        else
        {
            this.textLines.Add(lines[i]);
        }
    }    

然后我将每个解析的数据放入一个不同的.txt文件:

    using (StreamWriter emailTxt = new StreamWriter(@"txt/" + "user_email.txt"))
    {
        emailTxt.Write(nek.clientEmail);
    }

    using (StreamWriter userTxt = new StreamWriter(@"txt/" + "user_data.txt"))
    {
        userTxt.Write(nek.clientUser + Environment.NewLine + unixTime);
    }

    using (StreamWriter imageTxt = new StreamWriter(@"txt/" + "user_images.txt"))
    {
        foreach (KeyValuePair<int, string> kp in nek.imageLines)
        {
                imageTxt.WriteLine(string.Format("{0:00}: {1}", kp.Key, kp.Value));
        }
    }

但是,我以某种方式检索了所有数据,除了imageTxt应该是:

http://www.1234567890.net/image/cc_141019050341.png

我正在接收:

05: 341.png

任何想法为何?谢谢您的时间。

c# visual-studio-2010
1个回答
0
投票

您的子字符串达到cc_141019050341.png

cc_141019(first one extracts this 05)0(second one extracts this 341.png )

我建议您使用正则表达式提取所需的部分

类似

IMAGE(?<num>\d+).*?:\s(?<url>.*)
© www.soinside.com 2019 - 2024. All rights reserved.