0

I need a help with my code, I'm trying to format a text in a string setting breaklines before the dates formats.

Example:

string formulario = "21/02/2011 - 15:02 - Albert Einsten - Won the lottery 21/11/2012 - 16:14 - Nicollas Tesla - Lost his keys. Keys Id: 0666793 ";

I need my string to be in the next format:

21/02/2011 - 15:02 - Albert Einsten - Won the lottery

21/11/2012 - 16:14 - Nicollas Tesla - Lost his keys. Keys Id: 0666793

4
  • 5
    Use a line break \n Or you can use @ before your string to conserve whatever line breaks you include in the string Commented Jun 6, 2018 at 15:02
  • Is the string formulario all one line like that? Or is there new line characters in it? Commented Jun 6, 2018 at 15:03
  • 2
    Or, better yet, Environment.NewLine Commented Jun 6, 2018 at 15:05
  • 1
    @awh112 your edit might not be accurate. The OP included a new line break in the original example and your edit removes that. Commented Jun 6, 2018 at 15:09

4 Answers 4

1

You need to use a line break \n or even better System.Environment.NewLine

Sign up to request clarification or add additional context in comments.

1 Comment

Environment.NewLine is better
0

Environment.NewLine is your friend. It's accurate for every environment as the framework chooses the appropriate line ending automatically and you can assign it to a string to use it in a faster way if you don't like its verbosity.

Like

string nl = Environment.NewLine;
string phrase = "now we go to a new line" + nl;

or if you use C# 6 at least you can use syntactic sugar with string interpolation:

 string nl = Environment.NewLine;
 string phrase2 = $"now we go to a new line{nl}And this is the new line{nl}And this is another one";

(or put Environment.NewLine directly inside brackets if you don't like the variable)

2 Comments

string nl = Environment.NewLine; feels like an unnecessary memory aloocation
It all depends on your tastes. I Specified that you can put directly Environment.NewLine without the variable allocation. I prefer to allocate it because it's more readable when it comes to use it many times in the code
0

Just use a verbatim string:

string formulario = @"21/02/2011 - 15:02 - Albert Einsten - Won the lottery 

21/11/2012 - 16:14 - Nicollas Tesla - Lost his keys. Keys Id: 0666793";

Or format your string using Environment.NewLine:

//string.Format will replace {0} with the result of Environment.NewLine
string formulario = string.Format("21/02/2011 - 15:02 - Albert Einsten - Won the lottery{0}{0}21/11/2012 - 16:14 - Nicollas Tesla - Lost his keys. Keys Id: 0666793", Environment.NewLine);

Or you can use C# 6 $ strings (shorthand for the above)

string formulario = $"21/02/2011 - 15:02 - Albert Einsten - Won the lottery{Environment.NewLine}{Environment.NewLine}21/11/2012 - 16:14 - Nicollas Tesla - Lost his keys. Keys Id: 0666793";

3 Comments

using the newest c#: $"21/02/2011 - 15:02 - Albert Einsten - Won the lottery{Environment.NewLine}{Environment.NewLine}21/11/2012 - 16:14 - Nicollas Tesla - Lost his keys. Keys Id: 0666793"
Yeah, they're not in my everday usage yet, added to answer, thanks @DontThinkJustGo
There are very few cases where string interpolation shouldn't be in common usage. The language feature is now almost 3 years old and is supported on Visual Studio 15 forward. For the most part, developers not using it at this point are simply falling back on what they've always done instead of allowing language features added to help produce more readable code do their job
0

I found a Solution using Regex

String pattern  = @" ([\d]{2}/[\d]{2}/[\d]{4})";
String Formulario = "21/02/2011 - 15:02 - Albert Einsten - Won the lottery 21/11/2012 - 16:14 - Nicollas Tesla - Lost his keys. Keys Id: 0666793 ";

formulario = Regex.Replace(formulario, pattern, Environment.NewLine + "$&");

Console.WriteLine(formulario.Replace("\n ", "\n"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.