Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to below output in C# or SQL Table

Input File Text
--------------
~
A
B
~
C
D

Output Text
--------------
A~B
C~D

What I have tried:

string[] lines = File.ReadAllLines("D:\\InputFile.txt");
List<string> list = new List<string>();
foreach (var item in lines)
{
    if (item=="~")
    {

    }

}
list.ForEach(i => Console.WriteLine(i));
Posted
Comments
Richard MacCutchan 30-Jan-24 6:27am    
When you see a "~" character you need to capture all following alpha characters. You then join them into a string with the tilde in between.
PIEBALDconsult 30-Jan-24 10:53am    
Yeah, we're gonna need a clearer spec, mmmkay?

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

And you haven't done any of the work: just set up a rough framework that might do the job if you fill in the actual code.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Seeing that you started showing some effort, I will help. Next time post way more code and explanations please, you will get more replies that way...

C#
static void Main()
    {
        string[] lines = File.ReadAllLines("D:\\InputFile.txt");
        List<string> list = new List<string>();

        //Start a string to hold the current line...
        string currentLine = "";

        foreach (var item in lines)
        {
            if (item == "~")
            {
                //Add the current line to the list when '~' sign is found...
                list.Add(currentLine);

                //Reset your current line for the next set of values...
                currentLine = "";
            }
            else
            {
                //Concatenate the current item to your current line...
                currentLine += item;
            }
        }

        //Add the last line to the list (assuming the file doesn't end with '~')...
        list.Add(currentLine);

        //Print your result...
        list.ForEach(i => Console.WriteLine(i));
    }
 
Share this answer
 
FWIW if you want to do something like this in SQL...

This may be what you want:
Using PIVOT and UNPIVOT - SQL Server | Microsoft Learn[^]
 
Share this answer
 
Comments
jochance 30-Jan-24 18:31pm    
Sorry this probably should've been a comment not a solution.

It seems to me that you can do the required formatting in one line of code.


C#
List<string> lines = ["~", "A", "B", "~", "C", "D","~","E","F"];
string[] formattedLines = string.Join('~', lines).Trim('~').Split("~~~");

A call to string.Join('~', lines) concatenates the lines into a single line where each line forms a substring separated by a '~' . At this stage the result is '~~A~B~~~C~D~~~E~F'.So all that is needed now is to Trim the two '~' from the start of the string and then split on '~~~'.

 
Share this answer
 
I would use ReadAllText rather than ReadAllLines. Then I'd split it into tokens with a RegularExpression and stick them back together.

C#
      string text = 
@"
~
A
B
~
C
D
" ;

      foreach 
      ( 
        string s 
      in 
        Rearranger
        (
          new System.Text.RegularExpressions.Regex 
          ( 
            @"(~)\r\n(\S+)\r\n(\S+)" 
          , 
            System.Text.RegularExpressions.RegexOptions.Compiled 
          ) 
        ,
          new int[] { 2 , 1 , 3 }
        ,
          text
        ) 
      )
      {
        System.Console.WriteLine ( s ) ;
      }


C#
private static readonly System.Text.StringBuilder buffer =
  new System.Text.StringBuilder() ;

private static System.Collections.Generic.IEnumerable<string>
Rearranger
(
  System.Text.RegularExpressions.Regex Splitter
,
  int[]                                GroupOrder
,
  string                               Input
)
{
  System.Text.RegularExpressions.MatchCollection mat = Splitter.Matches ( Input ) ;

  lock ( buffer )
  {
    for ( int i = 0 ; i < mat.Count ; i++ )
    {
      buffer.Length = 0 ;

      for ( int j = 0 ; j < GroupOrder.Length ; j++ )
      {
        buffer.Append ( mat [ i ].Groups [ GroupOrder [ j ] ].Value ) ;
      }

      yield return ( buffer.ToString() ) ;
    }
  }

  yield break ;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900