【问题标题】:Concatting Lines into List of strings将行连接成字符串列表
【发布时间】:2011-12-01 23:20:48
【问题描述】:

我有一个正在使用的文本文件(已加载到每个新行的列表中)。它的格式是这样的:

***CP6***
UNIT, PARTS
    some data here
    some more data here
        more data1
        more data2
    etc111
        etc222
    etc333
    etc444
    etc555
UNIT, PARTS
    11111
    22222
        2.1
        2.2
        2.3
    33333
and so on....

我想抓取每个 UNIT, PARTS 之间的行并将其连接到一行,如下所示:

theList[0] = UNIT, PARTS\n\tsome data here\n\tsome more data here\n\t\tmore data1\n\t\tmore data2\n\tetc111\n\t\tetc222\n\tetc333\n\tetc444\n\tetc555
theList[1] = UNIT, PARTS\n\t11111\n\t22222\n\t\t2.1\n\t\t2.2\n\t\t2.3\n\t33333
theList[n] = UNIT, PARTS\n\t.......

谁能帮我解决这个问题?

编辑:

数据在列表中。所以我在想foreach (var item in fileLineList)...

编辑2:

我一直在搞砸并想出了这个..但它似乎并没有按照我的预期工作......

                foreach (var line in tempList1)
                {

                    if (isUnitPart == false)
                    {
                        if (line.ToUpper().Contains("\"UNIT\",\"PARTS\""))
                            isUnitPart = true;
                    }
                    else
                    {
                        if (line.ToUpper().Contains("\"UNIT\",\"PARTS\""))
                            isUnitPart = false;
                    }

                    if (isUnitPart == true)
                        concattedUnitPart = concattedUnitPart + line;

                    else
                    {
                        theList.Add(concattedUnitPart + Environment.NewLine);
                    }
                }

【问题讨论】:

  • 每一项都应该以UNIT、PARTS或python缩进样式开头?
  • @sleimanjneidi:每个项目都应该以UNIT, PARTS开头

标签: c# string concatenation


【解决方案1】:
var units = new Regex("UNIT, PARTS",RegexOptions.Multiline)
          .Split(myString)
           .Where(c=> !string.IsNullOrEmpty(c)).ToArray();

【讨论】:

  • 但是分隔符本身("UNIT, PARTS") 将从返回的数组中删除。这不是 OP 所要求的。
【解决方案2】:

myString.Split(new string[]{"UNIT, PARTS"}, StringSplitOptions.None)会给你

theList[0] = \n\tsome data here\n\tsome more data here\n\t\tmore data1\n\t\tmore data2\n\tetc111\n\t\tetc222\n\tetc333\n\tetc444\n\tetc555
theList[1] = \n\t11111\n\t22222\n\t\t2.1\n\t\t2.2\n\t\t2.3\n\t33333
theList[n] = \n\t....

这可能是你想要的:)

【讨论】:

    【解决方案3】:

    使用ReadAllText 将所有行放在一个string 中。

    编辑:如果您的所有数据都在列表中:

    string[] input = IO.File.ReadAllLines(path); //or a List<String>  
    string[] delimiter = new[] { "UNIT, PARTS" };
    string text = String.Join(Environment.NewLine, input);
    var lines = from word in text.Split(delimiter, StringSplitOptions.None)      
                select line = (delimiter[0] + word)
    

    您需要在结果词前加上分隔符本身,因为String.Split 会从返回的数组中删除分隔符。

    http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx

    【讨论】:

    • 我在分隔符上出现错误:Cannont initialize an implicitly-typed local variable with an array initializer.
    • 我不知道这不会编译in C#,因为它在 VB.NET 中没有问题(通常 VB 更冗长)。这是从 VB 手动转换的。相应地编辑了我的答案。
    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 2020-02-21
    • 2015-09-18
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多