?
Solved

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

Posted on 2008-11-18
13
Medium Priority
?
330 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:djcheeky
  • 6
  • 4
  • 3
13 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22984547
try String a = "\r\n";
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22984564
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
 

Author Comment

by:djcheeky
ID: 22984635
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22984940
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
 

Author Comment

by:djcheeky
ID: 22985041
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
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22985444
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
 

Author Comment

by:djcheeky
ID: 22986753
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
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22993011
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
 

Author Comment

by:djcheeky
ID: 23002329
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
 

Author Comment

by:djcheeky
ID: 23002835
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
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 23003548
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
 

Author Comment

by:djcheeky
ID: 23003658
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
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 1500 total points
ID: 23004177
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question