C# 将字符串分隔到集合字典中不存在的列表中

C# 将字符串分隔到集合字典中不存在的列表中,c#,linq,C#,Linq,这是我的一句话: 您可以在StackOverFlow.com上提问 现在我有了一个字符串列表的字典集合: Dictionary<int, List<string>> collections = new Dictionary<int, List<string>>() { { 1, new List<string>() { "You", "your" } }, { 2, new List<string>() {

这是我的一句话:

您可以在StackOverFlow.com上提问

现在我有了一个字符串列表的字典集合:

Dictionary<int, List<string>> collections = new Dictionary<int, List<string>>()
{
    { 1, new List<string>() { "You", "your" } },
    { 2, new List<string>() { "Stack", "Flow" } },
};
但是不知道如何检查列表的整个字典集合,获取每个列表键并将其替换到新的字符串列表中

static void Main()
{
    string myString = "You ask your questions on StackOverFlow.com";
    string[] collection = new string[]
    {
        "You",
        "your",
        "Stack",
        "Flow"
    };

    var splitted = myString.Split(collection, StringSplitOptions.RemoveEmptyEntries);

    foreach(var s in splitted)
    {
        Console.WriteLine("\""+s+"\"");
    }
}
将导致:

" ask "
" questions on "
"Over"
".com"
您可以使用
SelectMany
展平您的
收藏


根据您的编辑:

static void Main()
{
    string myString = "You ask your questions on StackOverFlow.com";

    Dictionary<int, List<string>> collections = new Dictionary<int, List<string>>()
    {
        { 1, new List<string>() { "You", "your" } },
        { 2, new List<string>() { "Stack", "Flow" } },
    };

    foreach(var index in collections.Keys)
    {
        foreach(var str in collections[index])
        {
            myString = myString.Replace(str, index.ToString());
        }
    }

    Console.WriteLine(myString);
}

注意,开头的空字符串是

如果在输入字符串的开头或结尾找到匹配项, 字符串的开头或结尾包含一个空字符串 返回数组


但是您可以使用
.Where(s=>!string.IsNullOrEmpty(s))
或类似的方法轻松删除它。

您可以使用
SelectMany
linq函数展平列表。这个新的字符串列表可以提供给
Split
函数

var str = "You ask your questions on StackOverFlow.com";
var collections = new[]
{
    new List<string> { "You", "your" },
    new List<string> { "Stack", "Flow" },
};

var values = str.Split(
    collections.SelectMany(s => s).Distinct().ToArray(), 
    StringSplitOptions.RemoveEmptyEntries
);

Console.WriteLine(
    string.Join(", ", values)
);
var str=“您可以在StackOverFlow.com上提问”;
var collections=new[]
{
新列表{“你”,“你的”},
新列表{“堆栈”、“流”},
};
var值=str.Split(
collections.SelectMany(s=>s).Distinct().ToArray(),
StringSplitOptions.RemoveEmptyEntries
);
控制台写入线(
string.Join(“,”值)
);
上面的代码导致

在,.com上提问


您可以使用linq展平您的收藏:

var collections = new[]{
    new List<string> { "You", "your" },
    new List<string> { "Stack", "Flow" },
};

var target = "You ask your questions on StackOverFlow.com";

var splitparts = collections.SelectMany(list => list.Select(s => s)).Distinct().ToArray();

var result = target.Split(splitparts,StringSplitOptions.RemoveEmptyEntries);

Distinct()部分从您的集合中删除副本。

但不知道如何检查整个列表集合。

我对你问题的理解是,你在“收藏的收藏”方面遇到了麻烦。当其他人展示如何将列表集合压缩为单个列表时,能够搜索嵌套集合是一项非常有用的技能。我以这种精神提出这一点

        var myString = "You ask your questions on StackOverFlow.com";

        var collections = new[]
        {
            new List<string> { "You", "your" },
            new List<string> { "Stack", "Flow" },
        };

        foreach (var listOfStrings in collections)
        {
            foreach (var word in listOfStrings)
            {
                Console.WriteLine(myString.Contains(word) ? "Yes on " + word : "No on " + word);
            }
        }
var myString=“您可以在StackOverFlow.com上提问”;
var collections=new[]
{
新列表{“你”,“你的”},
新列表{“堆栈”、“流”},
};
foreach(集合中的var listofstring)
{
foreach(listofstring中的var字)
{
Console.WriteLine(myString.Contains(word)-“Yes on”+word:“No on”+word);
}
}

我只是在匹配项上输出“是”或“否”,但您可以从字符串中删除它们,或者用其他字符串替换它们,或者执行任何转换。我最感兴趣的是展示如何根据您问题的措辞循环浏览列表的集合[]。

-然后您需要将您的
集合
展平到一个独特的数组,然后在这里您可以go@Rafalon-你如何将StackOverFlow分解为Stack,Over,Flow?@bommelding你不应该用更多的问题更新你的问题,如果你想扩展你的问题,你应该创建一个新的。现在所有现有答案都无效…@Nofuzy我根据您更新的问题更新了我的答案您更新的答案将生成一个字符串,但我需要一个单独字符串的列表。如何修复它?@Nofuzy根据康拉德·克拉克的评论,也许你可以使用-with
分隔符
作为
String.Join(collections[index],“|”)
。然后需要遍历数组,用相应的值替换匹配字符串。明天我将编辑我的答案,包括that@Nofuzy这将是一种荣幸,但我真的不愿意把我的邮件给每个人,因为我已经收到了足够多的垃圾邮件,所以如果你有办法我可以给你,那么你不应该用更多的问题更新你的问题,如果你想扩展你的问题,你应该创建一个新的。现在所有现有答案都无效。。。
""
"1"
" ask "
"1"
" questions on "
"2"
"Over"
"2"
".com"
var str = "You ask your questions on StackOverFlow.com";
var collections = new[]
{
    new List<string> { "You", "your" },
    new List<string> { "Stack", "Flow" },
};

var values = str.Split(
    collections.SelectMany(s => s).Distinct().ToArray(), 
    StringSplitOptions.RemoveEmptyEntries
);

Console.WriteLine(
    string.Join(", ", values)
);
var collections = new[]{
    new List<string> { "You", "your" },
    new List<string> { "Stack", "Flow" },
};

var target = "You ask your questions on StackOverFlow.com";

var splitparts = collections.SelectMany(list => list.Select(s => s)).Distinct().ToArray();

var result = target.Split(splitparts,StringSplitOptions.RemoveEmptyEntries);
" ask ", " questions on ", "Over", ".com"
        var myString = "You ask your questions on StackOverFlow.com";

        var collections = new[]
        {
            new List<string> { "You", "your" },
            new List<string> { "Stack", "Flow" },
        };

        foreach (var listOfStrings in collections)
        {
            foreach (var word in listOfStrings)
            {
                Console.WriteLine(myString.Contains(word) ? "Yes on " + word : "No on " + word);
            }
        }