0

I know this is kind of easy question but i cant seem to find it anywhere. Is there someone out there who knows how to create a soft return inside a set of text using C#.net?

I need to print soft return to a text file/xml file. this text file will be generated using c#.net. you could verify if the answer is correct if you use NOTEPAD++ then enable the option to “View>Show Symbol > Show End of Line” then you will see a symbol like this:

enter image description here

Thanks in advance :)

5
  • Please clarify your question. Do you just need to insert a newline at some place? Then Environment.NewLine is the way to go
    – M.Stramm
    Commented Jun 11, 2012 at 0:28
  • 2
    IIRC, the old DOS "soft return" was ASCII character 141 (where a CR was ASCII 13 and LF was ASCII 10 (0x0D and 0x0A respectively). I'm not positive my memory is right, though; it's been ages since I had to deal with that stuff, which is why I'm not posting it as an answer. I'm alsn not sure that's what you're really looking for; I think what you're looking for is an embedded return (CR/LF) in the csv file.
    – Ken White
    Commented Jun 11, 2012 at 0:54
  • @KenWhite this is what im looking at right now :) nice idea from you ;) i just have to try it can you please post you're answer?
    – Allan Chua
    Commented Jun 11, 2012 at 0:55
  • 1
    @Allan, I'm not sure how to do it in C#; I do C# work on occasion, but I primarily use Delphi (in Delphi, it's really easy - you just append #141 to the string; not sure how to do the same in C#). Perhaps someone else can demonstrate that in an answer. :-)
    – Ken White
    Commented Jun 11, 2012 at 1:01
  • if you want to follow Ken White's answers you could just append (char)141; Commented Jun 11, 2012 at 1:26

4 Answers 4

3
var message = $"Tom{Convert.ToChar(10)}Harry";

Results in:

Tom

Harry

With just a line feed between.

1
  • string s = $"Tom{(char)10}Harry"; //Same answer, just character conversion shorter syntax. There are 4 "characters", probably technically tokens, in ascii that feed text to the next line: 10 Line Feed, 11 Vertical Tab, 12 Form Feed, 13 Carriage Return. 0 - 31 are the special tokens. rapidtables.com/code/text/ascii-table.html
    – Mike Grimm
    Commented Aug 8, 2023 at 16:07
2

Not sure what you mean by a soft return. A quick Google search says it's a non-stored line break typically due to word wrapping in which case you wouldn't actually put this in a string, it would only be relevant when the string was rendered for display.

To put a carriage return and/or line feed in the string you would use:

string s = "line one\r\nline two";

And for further reference, here are the other escape codes that you can use.

Link (MSDN Blogs)

In response to your edit

The LF that you see can be represented with \n in a string. Obviously you have a specific line ending sequence that you need to represent. If you were to use Environment.NewLine that is going to give you different results on different platforms.

5
  • nonono, not in C#. Your should use Environment.NewLine. Also in windows it's \n\r, in linux its \n, on mac it was iirc \r\n, but now is also \n
    – M.Stramm
    Commented Jun 11, 2012 at 0:30
  • Actually yes that works in C# and on Windows the standard sequence is \r\n not \n\r as you state. Yes, there are platform-specific line endings, but unless he's using Mono or Silverlight, it's unlikely to come up.
    – Josh
    Commented Jun 11, 2012 at 0:33
  • 1
    It is \r\n on Windows, and Environment.NewLine is there for a reason. Suggest it be used.
    – Eric J.
    Commented Jun 11, 2012 at 0:34
  • Crap, you are right. My mistake, it is \r\n in Windows. But I still think we miss the OP's intention
    – M.Stramm
    Commented Jun 11, 2012 at 0:37
  • Somebody downvoted me too for no apparent reason. You should be required to add a comment if you are going to downvote something. Commented Jun 11, 2012 at 16:50
0

Lke already mentioned you can use Enviroment.NewLine but I am not sure if that i what you want or if you are actually trying to append a ASCII 141 to your string as mentioned in the comments.

You can add ASCII chr sequences to your string like this.

var myString = new StringBuilder("Foo");
myString.Append((char)141);
1
  • So no reasoning for the downvote? If you downvote at least leave a comment why Commented Jun 11, 2012 at 16:49
0

The soft enter is applied e.g. in MS Word as Shift+Enter, and can be reproduced programmatically using \v

var test = "This is a\vsoft enter";

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.