Home > Software engineering >  C# split two lines into one from text
C# split two lines into one from text

Time:05-31

I have an text file, and I read the text from the file and write to the string array. I put text example.

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

I need to have first and second line in one and so on․ For example first line like this

000001 #DCC##EMB#4083 0600 1150 0028 05/26 TEST DIGITAL TEST DIGITAL 985 #END#@@@@@@

second line like this 000002 #DCC##EMB#5501 0401 0015 5406 05/26 TEST DIGITAL TEST DIGITAL 820 #END#@@@@@@ and so on.

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}        
}

CodePudding user response:

The term you're looking for is not split, but join. Anyway, assuming you have a list or array called lines in which each line is already loaded:

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

Or, corresponding with the code added to the question in an edit:

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]);
  }
}

CodePudding user response:

Here I have a extension method to get what you want using an 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;
    }
}

Example use

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

var newLines = lines.GetEach2Lines();

EDIT

According to your example

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)`;
    }
}
  • Related