如何将列表项拆分为以特定值开头的列表?

时间:2017-02-20 04:24:03

标签: c#

我有以下列表。

var mainList = new List<string>
{ "Reset","Set","Test","Test","Reset","Test","Test"};

我想将此主列表拆分为两个列表,每个列表都会启动&#34;重置&#34;。 喜欢,

{ "Reset","Set","Test","Test"} and {"Reset","Test","Test"}

如何拆分列表?

7 个答案:

答案 0 :(得分:2)

如果我理解正确你想做这样的事情

var mainList = new List<string> { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };
List<string> jask = new List<string>();
List<string> jask1 = new List<string>();
jask = mainList.Take(4).ToList();
jask1 = mainList.Skip(4).ToList();

后来我得知你想通过“重置”将它拆分,而不是像这样做

var mainList = new List<string> { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };
List<string> jask = new List<string>();
string ksjd =  string.Join(",", mainList.ToArray());
jask = Regex.Split(ksjd, @"(?=Reset)").Skip(1).ToList();

答案 1 :(得分:1)

以下解决方案假设您在“重置”文本之前拆分,因此如果第一个字段不是“重置”,那么它将跳过所有元素,直到找到“重置”。

  public static List<List<string>> Split(this List<string> list, string splitter)
        {
            var _list = new List<List<string>>();
            var count = list.Count(x => x == splitter);
            list.ForEach(item =>
            {
                if(item == splitter)
                {
                    _list.Add(new List<string>());
                }
                _list.LastOrDefault()?.Add(item);
            });
            return _list.ToList();
        }

用法

 var mainList = new List<string>
                          { "Set","Test","Test","Reset","Test","Test"};
 var res = mainList.Split("Reset");

答案 2 :(得分:0)

我认为您必须在此处使用词典来获取结果组,如下所示:

var mainList = new List<string> { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };
Dictionary<int, List<string>> resultList = new Dictionary<int, List<string>>();
int DictionaryIndex = 0;
foreach (string item in mainList)
{
    if (item == "Reset")
    {
        resultList.Add(++DictionaryIndex, new List<string>() { item });
    }
    else
    {
        resultList[DictionaryIndex].Add(item);
    }
}

resultList将根据您的需要包含单独的列表。您可以查看此Example,并告诉我它是否满足您的要求。

答案 3 :(得分:0)

试试这个方法: https://msdn.microsoft.com/en-us/library/1f4bkxt7(v=vs.110).aspx

//伪代码

while (index < len)
{
  int nextOccurence = arrayList.IndexOf(delimeterString, index)
  output.Add (arrayList.GetRange(index, nextOccurence))
  index = nextOccurence + 1
}

答案 4 :(得分:0)

var mainList = new List<string> { "Reset", "Set", "Test", "Test", "Reset",        "Test", "Test" };
        var lastIndex = mainList.FindLastIndex(x => x.Equals("Reset"));
        var firstList = new List<string>();
        var secondList = new List<string>();
        for (int i = 0; i < mainList.Count - 1; i++)
        {
            if (i < lastIndex)
            {
                firstList.Add(mainList[i]);
            }
            else
            {
                secondList.Add(mainList[i]);
            }
        }

答案 5 :(得分:0)

试试这个......

public static void SplitList()
    {
        var mainList = new List<string> { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };

        List<List<string>> lstOutputLists = new List<List<string>>();
        List<string> tmp = new List<string>();
        foreach (var item in mainList)
        {
            if (item == "Reset")
            {
                if (tmp.Count != 0)
                {
                    lstOutputLists.Add(tmp);
                    tmp = new List<string>();    
                }

            }
            tmp.Add(item);
        }
        lstOutputLists.Add(tmp);
    }

lstOutputLists将为您提供输出..

答案 6 :(得分:0)

这样的事情:

var result = string
    .Join("|", mainList)
    //     ^ use a special character/pattern that will never use in your texts
    .Replace("Reset", "|@|Reset")
    //                 ^^^ use a special pattern for identifying place of `Reset`  
    .Split(new [] { "|@|" }, StringSplitOptions.RemoveEmptyEntries)
    // at first I split by place of `Reset` to create a list of strings that identifies by `|@|`
    .Select(c => c.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
    // now each string will become a list of strings
    .ToList();

[ C# Demo ]

在LinQ-full模式中,我也可以建议:
注意:Thjis方法与SQL中的Partition By类似。

var i = 1;
var result = mainList
    .Select(str =>
    {
        if (str == "Reset") i++;
        return new { str, i };
    })
    //  ^^ Above I make partitions by `i`; that `i` will change by watching a `Reset`
    .GroupBy(g => g.i)
    .Select(g => g.Select(c => c.str).ToList());
    // then I just group by `i` as partition then removing it from results.

[ C# Demo ]