0

I noticed recently that when I used Console.WriteLine on any string larger than 80 characters (or larger than the buffer size) the output would break the string exactly at 80 characters.

If this was really 80 characters the break wou
ld look something like that.

While I know I could use some regex craziness to bypass this problem, I was wondering if there was a simpler solution. I have googled it, but I can't find any code or functions for C# that keep the console from breaking up words.

WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
HadesHerald
  • 131
  • 1
  • 7
  • You could simply come up with a method that when it reaches 80 characters, checks for the (nearest) whitespace (`" "`) and replaces it with a break (`"\r\n"`) – Ben Aug 13 '15 at 22:57
  • Side note: Optimally fit text into fixed size layout is very common interview question - there should be plenty answers (include "dynamic programming" in you search query to get better results). – Alexei Levenkov Aug 13 '15 at 22:58
  • Ok, that would probably help, thank you – HadesHerald Aug 13 '15 at 22:59
  • http://stackoverflow.com/questions/17586/best-word-wrap-algorithm – Alexei Levenkov Aug 13 '15 at 23:01

1 Answers1

0

use this pattern (I used 40 characters in this case, change to 80)

^(.{1,40})\s

and replace with $1\r\n Demo

# ^(.{1,40})\s
^               # Start of string/line
(               # Capturing Group (1)
  .             # Any character except line break
  {1,40}        # (repeated {1,40} times)
)               # End of Capturing Group (1)
\s              # <whitespace character>
alpha bravo
  • 7,838
  • 1
  • 19
  • 23