How to evaluate escape charaters such as \n and \t from the database value

Hi.

Given two strings:

String a = "\n";
String b = someValueSavedInTheDb; //obtained from a value stored in the DB

If I try and run the following code:
//for a
Console.WriteLine("Delimiter:" + a + "EndText");
//for b
Console.WriteLine("Delimiter:" + b + "EndText");

The result is:

//for a
Delimiter:
EndText

//for b
Delimiter:\nEndText

As you can see, in Case a, the \n is correctly translated into a newline, however, when case b is executed, with a value obtained from the database, the Console outputs \n instead of a newline.
How do I get case b's output to be the same as case b's? In other words - case b also looks like:

Delimiter:
EndText

This happens for all values that are preceded by a backslash, including:
\n
\t
\r

etc.

Please help.
Thanks
djcheekyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
try String a = "\r\n";
0
Tony McCreathTechnical SEO ConsultantCommented:
It sounds like you have a decoding or encoding issue.

Could you explain how you are getting the data from the database? adapter, reader?

Could you confirm that the data in the database has not been double escaped. i.e. newlines ended up as \\n
0
djcheekyAuthor Commented:
Hi Tiggerito

I am loading the data via:
IDataReader reader = DataHelper.Database.ExecuteReader("spGetDetail", theId));

The data in the database is stored without escape characters, i.e.
\n
\t
\r
etc

Thanks
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tony McCreathTechnical SEO ConsultantCommented:
Some how the data you read in is of the form \\n which will get displayed as "\n" instead of as a new line.

You could do a replace to get rid of the extra slash, but it would be better to find out where the data is getting encoded/escaped twice.
0
djcheekyAuthor Commented:
I will do a count on the '\' backslash characters to see how many are in the string before it is outputted and get back to you.
0
Anurag ThakurTechnical ManagerCommented:
i will suggest you to use System.Environment.NewLine in places you know a new line is going to come because sometimes \n does not work
http://msdn.microsoft.com/en-us/library/system.environment.newline(VS.80).aspx
0
djcheekyAuthor Commented:
Thanks ragi but my problem is not the newline. It is the fact that the system is not interpreting \n , \t, \r etc as it should.
0
Anurag ThakurTechnical ManagerCommented:
yes i know that as i have faced the same issue and i overcame the new lines \n and \r by using system.environment.newline only
0
djcheekyAuthor Commented:
If I write the output of b to the Console or to File, I get "\n".

But if I do:

String.Compare(b,"\n")

I get 1, which means they are not equal? It should be 0.

How can I view the actual contents / value of b??


0
djcheekyAuthor Commented:
Ok, if I do the code snippet below:

I get the output:

[        ]

Which is correct. But what I don't understand is that I am in fact replacing one string with the exact same string and then the program works???

Why is this?



 String tDelim = dataChunkDelimiter;
            if (String.Compare(dataChunkDelimiter, "\\t") == 0)
            {
                tDelim = "\t";
                Console.WriteLine("[" + tDelim + "]");
            }

Open in new window

0
Tony McCreathTechnical SEO ConsultantCommented:
You are checking for the string sequence slash + t which is different to the tab character you are replacing it with.

If that is solving your problem them my earlier assumption is probably right. The data is being stored as the string "\t" and not a tab.

Could you provide the code you use to read the data from the database. We could then eliminate that from the equation.

If you have a database tool. edit some data and enter a tab. See if the tool displays that tab in the same way the other tabs are displayed. This woudl confirn if the data in the database is correct or not.


0
djcheekyAuthor Commented:
Well, when I put the tab into the database, the value is taken from a text input and I do indeed type in "\t".

What I am doing is allowing the user to decide which delimiter they would like to use, and they type that into a normal web textbox and that then gets saved to the database. So they could type  in:

\n
\r
\t

Anything they like really.

So as far as I understand, the string \t is actualy getting saved as opposed to a tab. If you try and put in a tab (or click tab) on the textbox, it moves to the next form field.

Anyway, the method I use to get the data, as I mentioned above, is the IDataReader. Then I use the following to populate the value:
      dataChunkDelimiter = (String)reader["dataChunkDelimiter"];

Thanks

0
Tony McCreathTechnical SEO ConsultantCommented:
Good,

So your code is doing what it should do. However you are wanting your users to be able to enter certain escape codes in the text box and have them convert to the related characters.

web text boxes do have trouble with things like tabs!

I would decide what character sequences the text box will accept for things like tab and convert the text box data before storing it in the database.

The reason for this is that your core data should be independent of the restrictions and formatting of particular display methods as much as possible.

The only sequence you need to support is tab. You can have multiline text boxes on the web that support the enter key and new lines.  aka <textarea>

So an option is to do a text.Replace("\\t","\t") before you save the data and provide the user with.

Another solution may be to add a tab button to enable users to insert real tabs. This page seems to provide a way to insert tabs into a textarea:

http://l4x.org/261/ 

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.