views:

563

answers:

6

Ok,

I have a string in a sql table like this

hello /r/n this is a test /r/n new line.

When i retrieve this string using c# for a textbox using multiline i expect the escape chars to be newlines.

But they are not and what happens is i get the line exactly as it is above.

It seems that the string returned from the table is taken as literal but i want the newlines!

How do i get this to work?

Thanks in advance..

+3  A: 

/ is not the escape character, \ is. What you need is:

hello \r\n this is a test \r\n new line.

Nick Whaley
sorry my error, the string is hello \r\n this is a test \r\n new line.Still get the same issue!
A: 

Things like "\n" (not "/n" by the way) are escape characters in programming languages, not inherently in text. (In particular, they're programming-language dependent.)

If you want to make

hello\r\nthis is a test\r\nnew line

format as

hello
this is a test
new line

you'll need to do the parsing yourself, replacing "\r" with carriage return, "\n" with newline etc, handling "\" as a literal backslash etc. I've typically found that a simple parser which just remembers whether or not the previous character was a backslash is good enough for most purposes. Something like this:

static string Unescape(string text)
{
    StringBuilder builder = new StringBuilder(text.Length);
    bool escaping = false;
    foreach (char c in text)
    {
        if (escaping)
        {
           // We're not handling \uxxxx etc
           escaping = false;
           switch(c)
           {
               case 'r': builder.Append('\r'); break;
               case 'n': builder.Append('\n'); break;
               case 't': builder.Append('\t'); break;
               case '\\': builder.Append('\\'); break;
               default:
                   throw new ArgumentException("Unhandled escape: " + c);
           }
        }
        else
        {
           if (c == '\\')
           {
               escaping = true;
           }
           else
           {
               builder.Append(c);
           }
        }
    }
    if (escaping)
    {
        throw new ArgumentException("Unterminated escape sequence");
    }
    return builder.ToString();
}

There are more efficient ways of doing it (skipping from backslash to backslash and appending whole substrings of non-escaped text, basically) but this is simple.

Jon Skeet
Thanks, you have saved me a lot of time...
A: 

I had a similar problem with my SQLite database the other day. Users are able to enter multi-line text into a textbox, and when it's inserted into the database, it goes in like:

"This is \r\n a multiline test"

It was coming out of System.Data.SQLite as:

"This is \\r\\n a multiline test"

I ended up using:

String.Replace("\\r","\r").Replace("\\n","\n")

to replace each of the escaped characters, which formatted the text properly when displayed in either a multi-line textbox or label.

Jared Harley
What happens if someone wants to enter "old\new" which would presumably end up as "old\\new" in the database? :)
Jon Skeet
Good point - something I hadn't thought of :) Thanks for pointing that out!
Jared Harley
A: 

Looks like you need to use System.Web.HttpUtility.HtmlDecode on the string before putting it in the TextBox.

 textBox.Value = HttpUtility.HtmlDecode(myString);
Koistya Navin
Are you sure this will work for escape chars?
@Stuart, sure, why not? This helper function exists expecially for encoding/decoding html strings. HttpUtility.HtmlEncode(..), HttpUtility.HtmlDecode(..)
Koistya Navin
A: 

I do not see why a Parser is required in this instance, given that the language in question is C# and I assume that the OS is 32-bit Windows which treats \r\n as the NewLine character.

The following simple code (Windows app) works for me:

string s = "hello \r\n this is a test \r\n new line.";

// Set Multiline to True.
textBox1.Multiline = true;

// Expand the height of the textbox to be able to view the full text.
textBox1.Height = 100;
textBox1.Text = s;

The textbox shows text as:

hello
 this is a test 
 new line.
Cerebrus
The programming language is C#, but the text isn't coming from a C# source file - it's coming from a database. Your case has a parser in as well - the C# compiler.
Jon Skeet
@Cerebrus - I think my post might explain why your code example works correctly.
Stevo3000
No doubt possible, but that is the OP's responsibility to provide accurate information, not leave us guessing.
Cerebrus
A: 

I think the issue here is that the string as seen by the user is

hello \r\n this is a test \r\n new line.

while the C# code sees it as

"hello \\r\\n this is a test \\r\\n new line."

A much simpler solution would be the following

s = Regex.Replace(s, @"\\\\", @"\");

which replaces @"\\" with @"\". Don't be confused by the regex match pattern, the \ has to be escaped in the match but not the replacement.

Stevo3000