Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hey there, this is my new challenge:

I have some info in a plain text file. I want to recover it and publish it to the user using C#. This text has some escape commands such as \n or \t. I tried with a RichTextBox and it doesn't work :thumbsdown:.

Is there any method to do it? I know there is, but I can't find it :(
Posted
Updated 28-Jan-11 12:34pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Jan-11 18:34pm    
Text has not commands, so there are no "escape commands" -- they are just regular character. The syntax you show is just C# (and C/C++ and a lot more) syntax for string literals.
Where is the problem? What "doesn't work"? It does work. You simply doing something wrong, so you need to show what you try to do. What did you try to achieve? -- Code, please!
--SA
pancho2413 28-Jan-11 18:51pm    
Thanks for your ultra-fast answer. The code is like this:

String mytext = "\nHello\n\n\tHow are you?"; //here I have 3 escape commands

After this, I add some escape commands for my dot matrix printer. Then, I do the following (supposing that the next control is a RichTextBox object):

richtextboxmessage.Text = mytext;

And when I run my project, the richtextboxmessage has some weird characters, maybe 10 characters and nothing else.

Now I tried not to add those escape commands for my printer and it ran :-P

I'm so happy because you were so fast. Thanks and I'll give you what you deserve for it.

If you want to get rid of all these escaped characters then you could use the method suggested in this thread from MSDN.Social[^].
 
Share this answer
 
Create a Textbox on your form (assuming you are using Windows Forms), then use this code:
C#
textBox1.Multiline = true;
textBox1.Dock = DockStyle.Fill;
textBox1.Text = "\r\nHello\r\n\r\n\tHow are you?";

In Windows, a new line is represented by "\r\n" rather than just "\n". If you have a file that contains "\n" characters, you can use a regular expression or String.Replace to fix the characters for display:
C#
textBox1.Text = "\nHello\n\n\tHow are you?".Replace("\n", "\r\n");

Or:
C#
textBox1.Text = System.Text.RegularExpressions.Regex.Replace("\nHello\n\n\tHow are you?", "(?<!\r)\n", "\r\n");


EDIT: Removed some testing code I forgot in there.
 
Share this answer
 
v2
Comments
Henry Minute 28-Jan-11 19:26pm    
As soon as I saw that you had answered this, I just knew it would be regex. :))
AspDotNetDev 28-Jan-11 19:44pm    
Uh oh, seems I've become "that damn regex guy" :-)

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