C# 将文本中的两行拆分为一行

C# split two lines into one from text

我有一个文本文件,我从文件中读取文本并写入字符串数组。我把文本示例。

000001
#DCC##EMB#4083 0600 1150 0028 05/26 测试数字测试数字 985 #END#@@@@@@
000002
#DCC##EMB#5501 0401 0015 5406 05/26 测试数字测试数字 820 #END#@@@@@@
000003
#DCC##EMB#4083 1001 0096 0020 05/26 测试数字测试数字 008 #END#@@@@@@
000004
#DCC##EMB#9051 1952 0000 0971 05/26 测试数字测试数字 145 #END#@@@@@@
000005
#DCC##EMB#4083 0600 1150 0010 05/26 测试数字测试数字 785 #END#@@@@@@

我需要第一行和第二行合二为一․例如像这样的第一行

000001 #DCC##EMB#4083 0600 1150 0028 05/26 测试数字测试数字 985 #END#@@@@@@

第二行像这样000002 #DCC##EMB#5501 0401 0015 5406 05/26 测试数字测试数字 820 #END#@@@@@@ 等等。

public async Task ProcessDataAsync(Stream stream)
{
  Memory<byte> array = new byte[stream.Length];
  var bytesRead = await stream.ReadAsync(array);
  _ = array.Slice(0, bytesRead);             
  string textFromFile = Encoding.Default.GetString(array.Span);             
  string[] allRows = textFromFile.Split(new string[] { "\r\n", 
  "\r", "\n" }, StringSplitOptions.None);             
  for (int i = 0; i < allRows.Length; i++)             
     {// here I need to retrieve two lines as one}        
}

您要找的不是split,而是join。 无论如何,假设您有一个名为 lines 的列表或数组,其中每一行都已加载:

for(int i = 1; i < lines.Count; i += 2)
{
    string merged = lines[i - 1] + " " + lines[i];
    // Do something with 'merged'
}

或者,对应于编辑中添加到问题的代码:

public async Task ProcessDataAsync(Stream stream)
{
  Memory<byte> array = new byte[stream.Length];
  var bytesRead = await stream.ReadAsync(array);
  _ = array.Slice(0, bytesRead);             
  string textFromFile = Encoding.Default.GetString(array.Span);             
  string[] allRows = textFromFile.Split(new string[] { "\r\n", 
  "\r", "\n" }, StringSplitOptions.None);             
  var merged = new List<string>();
  for (int i = 1; i < allRows.Length; i += 2) 
  {
    merged.Add(lines[i - 1] + " "  + lines[i]);
  }
}

这里我有一个扩展方法,可以使用 IEnumerable<>

得到你想要的东西
public static IEnumerable<string> GetEach2Lines(this IEnumerable<string> lines)
{
    using var enumerator = lines.GetEnumerator();

    while (enumerator.MoveNext())
    {
        var firstLine = enumerator.Current;
            
        if (!enumerator.MoveNext())
        {
            yield return firstLine;
            break;
        }

        var secondLine = enumerator.Current;

        yield return firstLine + secondLine;
    }
}

示例使用

var lines = new List<string>()
{
    "00001",
    "line1",
    "00002",
    "line2"
};

var newLines = lines.GetEach2Lines();

编辑

根据你的例子

public static class Extensions
{
    public static IEnumerable<string> GetEach2Lines(this IEnumerable<string> lines)
    {
        using var enumerator = lines.GetEnumerator();

        while (enumerator.MoveNext())
        {
            var firstLine = enumerator.Current;

            if (!enumerator.MoveNext())
            {
                yield return firstLine;
                break;
            }

            var secondLine = enumerator.Current;

            yield return firstLine + secondLine;
        }
    }
}


public class Example
{
    public async Task ProcessDataAsync(Stream stream)
    {
        Memory<byte> array = new byte[stream.Length];
        var bytesRead = await stream.ReadAsync(array);
        _ = array.Slice(0, bytesRead);
        string textFromFile = Encoding.Default.GetString(array.Span);
        string[] allRows = textFromFile.Split(new string[]
        {
            "\r\n",
            "\r", "\n"
        }, StringSplitOptions.None);


        // using ext method 
        var newLines = allRows.GetEach2Lines(); // same as `Extensions.GetEach2Lines(allRows)`;
    }
}