1

I'm currently taking input from a registration form and download the input detail as a text file. the downloaded textfile result is : JimJone03/09/1998 00:00:00xxx@gmail.com 230436315

I want the result to be displayed in column format.

For example:

  • Jim Jone

  • 03/09/98

  • xxx@gmail.com

  • +23057xxx556

This is what I have tried:

public ActionResult Create(Information information)
{
    var byteArray = Encoding.ASCII.GetBytes(information.FirstName + "" 
                    + information.Surname + "" + information.DOB + "" 
                    + information.Email + " " + information.Tel);

    var stream = new MemoryStream(byteArray);

    return File(stream, "text/plain", "your_file_name.txt");
}
bhav
  • 57
  • 6
  • Does this answer your question? [Save and load MemoryStream to/from a file](https://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file) – Maxim Sagaydachny Dec 10 '19 at 05:52
  • Not really. the data save in the text file need to be in a column format. it actually in horizontal format with no space – bhav Dec 10 '19 at 05:56

2 Answers2

1

You can try to use regex to match them:

var input = "Jim Jone 03/09/98 xxx@gmail.com +23057897765";

var name = new Regex("^[a-zA-Z\\s]+").Match(input);

var birthday = new Regex("\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}").Match(input);

var email = new Regex(@"([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)").Match(input);

var tel = new Regex("\\+\\d+").Match(input);

Console.WriteLine("Input: " + "\"" + input + "\"");
Console.WriteLine("Name: " + "\"" + name.Value.Substring(0, name.Value.Length - 1) + "\"");
Console.WriteLine("Birthday: " + "\"" + birthday.Value + "\"");
Console.WriteLine("Email: " + "\"" + email.Value + "\"");
Console.WriteLine("Tel: " + "\"" + tel.Value + "\"");

Output:

enter image description here

Then, you can write them all to your text file line-by-line.

Tân
  • 1
  • 15
  • 56
  • 102
1

Why don't you just use Environment.NewLine?

var byteArray = Encoding.ASCII.GetBytes($"{information.FirstName}{Environment.NewLine}{information.Surname}{Environment.NewLine}{information.DOB}{Environment.NewLine}{information.Email}{Environment.NewLine}{information.Tel}");

This should add a 'new line' after each value in your file.

Ronald
  • 1,990
  • 6
  • 24
  • 39