从字符串中拆分和匹配值

时间:2016-05-22 17:08:14

标签: c#

好吧,你们这些人过去对我很了不起,我不能完全感谢你们。我正在制作一个程序来帮助人们玩ARK:Survival Evolved来管理他们的专用服务器。

在.map文件中,我们现在将关注两个字符串值; TamerName,这是驯服恐龙的用户的名字,然后是DinoName,你可以猜到它是恐龙的名字。

字符串的设置方式不能只是搜索TamerName的实例,然后列出该用户下的Dino,而是列在整个文件本身。我想要做的是有一个TabControl,它创建以用户命名的单个选项卡,然后在该选项卡内填充一个面板,其中包含由该用户驯服的恐龙。

文件始终存储信息 TamerName      -DinoName

所以它看起来像这样(作为一个例子)

布兰登      -Derp 凯蒂      -Derpette 布兰登      -Derp2 布兰登      -Derp3 凯蒂      -Derpette2

等等。我希望它像这样列出

Tab1(TabName = Brandon)      -Derp      -Derp2      -Derp3 Tab2(TabName = Katie)      -Derpette      -Derpette2

等等。

我目前有一种方法可以将结果转储到List作为Name-Dino

如果需要,我可以提供相应的代码。希望我解释一切正常!!!

2 个答案:

答案 0 :(得分:1)

由于您已经有一个格式为“Name-Dino”的List,一种方法是创建一个新的数组或Tamers列表。在此示例中,listOfNamesAndDinos是您现有的列表。

List<string> tamerList = new List<string>();

foreach(string tamerDino in listOfNamesAndDinos)
{
    string tamerName = tamerDino.Split("-")[0].Trim();
    if(!tamerList.Contains(tamerName))
    {
        tamerList.Add(tamerName);
    }
}

//Create tabs using each name from list of tamers

foreach(string tamer in tamerList)
{
    List<string> fullDinosForTamer = listOfNamesAndDinos.Where(e => e.StartsWith(tamer)).ToList();
    //Populate your panel with the newly found dinos
}

此方法要求您在命名空间声明中 using System.Linq

答案 1 :(得分:1)

非常感谢@Alfie Goodacre给了我基础来解决这个问题:)我不得不稍微调整一下代码,我在下面提供了这些代码。如果有人能想到更好,更紧凑的写作方式,我愿意接受建议!谢谢!!

private void openToolStripMenuItem2_Click(object sender, EventArgs e)
    {
        OpenFileDialog ark = new OpenFileDialog();
        ark.Filter = "ARK Map (*.ark)|*.ark";
        int p = 0;
        if (ark.ShowDialog() == DialogResult.OK)
        {
            List<string> findings = new List<string>();
            for (int i = 0; i < 15; i++)
            {
                findings.Add(string.Format("{0}-{1}", Tamed_Dino(ark.FileName, Dino_Tamer, 37)[i], Tamed_Dino(ark.FileName, Dino_Name, 35)[i]));

            }
            List<string> tamerList = new List<string>();

            foreach (string tamerDino in findings)
            {
                string tamerName = tamerDino.Split('-')[0].Trim();
                if (!tamerList.Contains(tamerName))
                {
                    tamerList.Add(tamerName);
                    Console.WriteLine(tamerName);
                }
            }
            for (int l = 0; l < tamerList.Count; l++)
            {
                string title = tamerList[l];
                TabPage tabPage = new TabPage(title);
                multi_prof.TabPages.Add(title);
                Panel np = new Panel();
                np.BorderStyle = BorderStyle.None;
                np.Location = new Point(10, 10);
                np.Dock = DockStyle.Fill;
                np.Name = tamerList[l];
                multi_prof.SelectedIndex = l;
                multi_prof.SelectedTab.Invoke((Action)(() => multi_prof.SelectedTab.Controls.Add(np)));

                foreach (string tamer in tamerList)
                {
                    List<string> fullDinosForTamer = findings.Where(r => r.StartsWith(tamer)).ToList();
                    //Populate your panel with the newly found dinos
                    Label lbl;
                    p = 0;
                    for (int i = 0; i < fullDinosForTamer.Count; i++)
                    {
                        if (fullDinosForTamer[i].StartsWith(tamerList[l]))
                        {
                            lbl = new Label();
                            lbl.Text = fullDinosForTamer[i].Split('-')[1];
                            lbl.Name = fullDinosForTamer[i];
                            lbl.AutoSize = true;
                            lbl.Location = new Point(10, p * 20);
                            np.Invoke((Action)(() => np.Controls.Add(lbl)));
                            p++;
                        }
                    }
                }
            }
        }
    }