Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字符串中拆分和匹配值_C# - Fatal编程技术网

C# 从字符串中拆分和匹配值

C# 从字符串中拆分和匹配值,c#,C#,好吧,你们过去对我来说太棒了,我非常感谢你们。我正在开发一个帮助人们玩方舟的程序:生存进化到管理他们的专用服务器 在.map文件中有两个字符串值,我们现在将重点关注它们;TamerName是驯服恐龙的用户的名字,还有恐龙名字,你可以猜到它是恐龙的名字 字符串的设置方式不是简单地搜索TamerName实例,然后在该用户下列出恐龙,而是在整个文件中列出。我想做的是有一个TabControl,它创建以用户命名的单个选项卡,然后在该选项卡内填充一个面板,其中包含该用户驯服的恐龙 该文件始终存储以下信息:

好吧,你们过去对我来说太棒了,我非常感谢你们。我正在开发一个帮助人们玩方舟的程序:生存进化到管理他们的专用服务器

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

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

该文件始终存储以下信息: 塔梅尔奈 -迪诺南

所以它可以像这样(作为一个例子)

布兰登 -德普 凯蒂 -德佩特 布兰登 -Derp2 布兰登 -Derp3 凯蒂 -德佩特2

等等。我想把它列成这样

Tab1(TabName=Brandon) -德普 -Derp2 -Derp3 Tab2(TabName=Katie) -德佩特 -德佩特2

等等

我目前有一个方法,可以将结果转储到名为Dino的列表中


如果需要,我可以提供代码。希望我能正确地解释这一切

由于您已经有了一个格式类似于“Name Dino”的列表,一种方法是创建一个新的数组或驯服者列表。在本例中,
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
}
List tamerList=newlist();
foreach(namesanddinos列表中的字符串tamerDino)
{
字符串tamerName=tamerDino.Split(“-”[0].Trim();
如果(!tamerList.Contains(tamerName))
{
tamerList.Add(tamerName);
}
}
//使用驯服者列表中的每个名称创建选项卡
foreach(tamerList中的字符串驯服器)
{
List fullDinosForTamer=listOfNamesAndDinos.Where(e=>e.StartsWith(tamer)).ToList();
//用新找到的恐龙填充面板
}

此方法将要求您在命名空间声明< < /p> <代码>中使用<代码> < /p> ,很大程度上感谢@ 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++;
                        }
                    }
                }
            }
        }
    }
private void openToolStripMenuItem2\u单击(对象发送方,事件参数e)
{
OpenFileDialog ark=新建OpenFileDialog();
ark.Filter=“方舟地图(*.ark)|*.ark”;
int p=0;
if(ark.ShowDialog()==DialogResult.OK)
{
列表结果=新列表();
对于(int i=0;i<15;i++)
{
结果.Add(string.Format(“{0}-{1}”),Tamed_Dino(ark.FileName,Dino_Tamer,37)[i],Tamed_Dino(ark.FileName,Dino_Name,35)[i]);
}
List tamerList=新列表();
foreach(搜索结果中的字符串tamerDino)
{
字符串tamerName=tamerDino.Split('-')[0].Trim();
如果(!tamerList.Contains(tamerName))
{
tamerList.Add(tamerName);
控制台写入线(tamerName);
}
}
for(int l=0;lmulti_prof.SelectedTab.Controls.Add(np));
foreach(tamerList中的字符串驯服器)
{
列出fullDinosForTamer=findings.Where(r=>r.StartsWith(tamer)).ToList();
//用新找到的恐龙填充面板
标签lbl;
p=0;
for(int i=0;inp.Controls.Add(lbl));
p++;
}
}
}
}
}
}

是的,如果您能提供用于转储结果的代码,那么我将只关注实现TabControl函数,这将有所帮助。