none
concatenate multiple lines into single line

    Question

  • use vb,vbscript or even dos to convert the ff:

    1 abc

    2def

    3 wxyz

    1 def

    2ghi

    3 abcd

    To the ff:

    1 abc2def3 wxyz

    1 def2ghi3 abcd

    Thanks.

    Saturday, August 09, 2014 4:08 AM

Answers

  • Try this.  The code is much simpler in c#

    Imports System.IO
    Module Module1
    
        Dim input As String = "1 abc" + vbCrLf + _
            "2def" + vbCrLf + _
            "3 wxyz" + vbCrLf + _
            "1 def" + vbCrLf + _
            "2ghi" + vbCrLf + _
            "3 abcd"
    
    
        Sub Main()
            Dim reader As New StringReader(input)
            Dim output As String = ""
            Dim lineCount = 0
    
            Dim inputLine As Object = reader.ReadLine
            While Not inputLine Is Nothing
                If lineCount <> 0 Then
                    'perform mod 3
                    If lineCount - (3 * Int(lineCount / 3)) = 0 Then
                        output = output + vbCrLf + inputLine
                    Else
                        output = output + inputLine
                    End If
                Else
                    output = inputLine
                End If
                lineCount += 1
                inputLine = reader.ReadLine
            End While
        End Sub
    
    End Module


    jdweng

    • Marked as answer by reybhoie Saturday, August 09, 2014 10:59 AM
    Saturday, August 09, 2014 6:02 AM

All replies

  • Try this.  The code is much simpler in c#

    Imports System.IO
    Module Module1
    
        Dim input As String = "1 abc" + vbCrLf + _
            "2def" + vbCrLf + _
            "3 wxyz" + vbCrLf + _
            "1 def" + vbCrLf + _
            "2ghi" + vbCrLf + _
            "3 abcd"
    
    
        Sub Main()
            Dim reader As New StringReader(input)
            Dim output As String = ""
            Dim lineCount = 0
    
            Dim inputLine As Object = reader.ReadLine
            While Not inputLine Is Nothing
                If lineCount <> 0 Then
                    'perform mod 3
                    If lineCount - (3 * Int(lineCount / 3)) = 0 Then
                        output = output + vbCrLf + inputLine
                    Else
                        output = output + inputLine
                    End If
                Else
                    output = inputLine
                End If
                lineCount += 1
                inputLine = reader.ReadLine
            End While
        End Sub
    
    End Module


    jdweng

    • Marked as answer by reybhoie Saturday, August 09, 2014 10:59 AM
    Saturday, August 09, 2014 6:02 AM
  • Thanks jdweng! would you mind showing me the C# version and also, can this routine be done using dos command line?
    Saturday, August 09, 2014 11:03 AM
  • jdweng, I noticed that you declared the string as is in your vb code, actually, those strings are in a text file. And instead of counting the number of lines, can it be made to look for "1" as the starting point always and those that are not (does not matter how many) will be appended to it? Many thanks. 
    Saturday, August 09, 2014 9:50 PM
  • The C# code is below.  Vbnet doesn't have a module function,  VBnet you can't easily test for null in StringReader class, and Vbnet you have to use VbLfCr instead of '\n'.  If your are reading from a file use the class StreamReader instead of StringReader.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static string input = "1 abc\n" +
                "2def\n" +
                "3 wxyz\n" +
                "1 def\n" +
                "2ghi\n" +
                "3 abcd";
    
            static void Main(string[] args)
            {
                StringReader reader = new StringReader(input);
    
                string output = "";
                int lineCount = 0;
    
                        
                string inputLine  = "";
                while((inputLine  = reader.ReadLine()) != null)
                {
                    if(lineCount != 0)
                    {
                        //perform mod 3
                        if ((lineCount % 3) == 0)
                        {
                            output = output + "\n" + inputLine;
                        }
                        else
                        {
                            output = output + inputLine;
                        }
                    }
                    else
                    {
                        output = inputLine;
                    }
                    lineCount += 1;
                }
            }
        }
    }
    
    


    jdweng

    Sunday, August 10, 2014 2:48 AM