how to capture double quotes in a ASP.NET page? - c#

I have a text box on my web page which I want to allow the user to type double quotes in.
It's a web report kind of deal.
So users can enter in
breaking
or
"breaking" if they want to capture the exact word.
The issue is, when I look at the variable when I try to do "breaking", I keep getting this:
"\"breaking bad\""
I need to get "breaking bad" ...
How do I go about doing this properly? Is there NOT a way to do this and therefore I have to simply do a replace?

The backslashes that you are seeing are just Visual Studio's way of displaying quotes inside a string when using the debugger to inspect a variable. You are getting the correct value. If you use the 'Text Visualizer' feature in the debugger to examine the string, you'll see that the backslashes aren't really there.

when I look at the variable when I try to do "breaking", I keep getting this:
"\"breaking bad\""
If by "looking" you mean in the debugger, then what you are seeing is the debugger escaping the quotes by adding the backslashes. If you output that string to the console, a form, database, etc. It will be surrounded by quotes just as you'd expect it to.

This is how the debugger displays it. If you want to see the real version quickly, click on the loupe next to the value you're displaying in the debugger.

Related

How to use extra quotations in a regex statement in c#

Below I have a regex statement I have been working on for quite awhile. The problem I am having is that their are a lot of quotations I am trying to parse out (I think that's the terminology I am looking for) so Visual Studio is freaking out about it. I have tried to fix this using escape characters, but it still won't recognize the whole phrase.
Here is the phrase without the escape characters:
string exceptionPattern = #"(?:(?:"([^"}]*)")|(\w+))\s*:\s*(?:(?:"([^"}]*)")|(\w+))";
With just this code in, nothing else, almost every line in my code gets affected. Here is the code using escape characters:
string exceptionPattern = #"(?:(?:\"([^\"}]*)\")|(\w+))\s*:\s*(?:(?:\"([^\"}]*)\")|(\w+))";
Once this comes into play, only this line is not working. In VS, the ([^\ part close to the beginning is not highlighted, meaning that it is not in quotes. Does anyone have any idea on how I can fix this problem?
This is the string I am trying to match. Note: THIS IS NOT JSON! I have confirmed it many times with the developer who made the database where this sample is coming from and he confirmed it is not JSON, so please do not try to use JSON on this. Also, the regex I have is trying to match displayException and the message after it, and also exception and the message after that including success false, using the quotes as a point of splitting
{"data":"","displayException":"Invalid Account Status. Please complete the registration process by clicking the verification link in your eTTek Dash Registration Verification email. Please contact 1-800-341-6184 M-F 9a-5pm CT for further assistance.","exception":"UNABLE TO LOGIN","success":false}
Inside a verbatim string, to escape a double quotes you must need to add another double double quotes near to that like "". So the compiler treats "" as a double quotes or otherwise it would treat " as an end of the verbatim string.

C# verbatim string literal not working. Very Strange backslash always double

I've tried for quite a long time to figure out whats going on but I've not found anything anywhere that someone besides me has ran into this issue.
I'm simply trying to hard code a path into a string. Easy stuff. Well for some reason
string fullPathSourceFile = #"c:\SQLSOURCE.txt";
is evaluating to c:\\SQLSOURCE.txt
I've tried everything to evaluated it to a single backslash remove the double quotes and it wont work. I even tried Replace(#"\\", #"\") and it has no affect. Anyone have any idea what's going on with my code that would force a double backslash when a single one should be evaluated? This is driving me nuts and it's so damn easy yet causing me a lot of frustration.
I'm then using the string variable below:
using (StreamReader reader = new StreamReader(fullPathSourceFile))
{
string line;
while ((line = reader.ReadLine()) != null)
{
sqlDBsource = line.ToString();
}
reader.Close();
}
Thanks to everyone for their input which helped my figure out what I was doing wrong. In Visual Studio (which is confusing) when you look at the value of a string in the debugger, it puts escapes in for you, so a double-backslash in a watch window or variable value popup is normal and does not mean there are actually two backslashes. When you mouse-over the variable or watch it in the watch window, click the magnifying glass icon at the right hand side of the tooltip/pane, this will show you the unescaped string at it would be printed to the console. Another way to display the actual results is: Console.WriteLine(the_problem_string); The issue I was having with the code is outside the scope of the post but the confusion of the results I was seeing from Visual Studio lead me to believe the string was the source of the problem when it wasn't.
This was a weird one. So I removed the verbatim as suggested in the comments and it worked when I used the double backslashes in the string. For some reason the code did not like the verbatim string and was translating the backslashes incorrectly. This resolved the issue. If anyone runs in to this you may need to play with the verbatim/non-verbatim strings because in some circumstances the compiler prefers non-verbatim.

Reading a string from a form as a verbatim string, \\ showing instead of \

I have the following property:
public string InstanceName
{
get
{
return cbServerInstanceName.Text;
}
}
where the input for
cbServerInstanceName.Text = "ServerName\ PcName"
This is showing up as
"ServerName\\ PcName"
I tried using the string.replace but couldnt get it to work.
Any ideas?
When you are using C# and looking at strings in the debugger, it will escape certain characters and \ is one of them; it will show in the debugger as \\ but at runtime and not viewed in the debugger, it will be converted to a single \
I'm going to take a stab at it and assume you're seeing "ServerName\\ PcName" using the debugger view in Visual Studio. Since it is showing you "a string\\" instead of a #"string literal\", you will see your slashes escaped. Just as you would with "\r\n" if you added a new-line.
If you can try printing your value to a MessageBox, or Debug or the Console. It should appear as you expect. fingers crossed

Could not find Path

I am giving path to my file which i want to read in my program like this, path = "c:/users/abcd/desktop/read.txt" but while debugging i found it's showing like c://users//abcd//desktop//read.txt which is an invalid path.
Is there something i am missing or how should i go about declaring a path.
I would just use:
#"c:\user\abcd\desktop\read.txt"
(note the use of the #). Another likely issue is permissions; does the app have access to user acbd's desktop? You might also look at:
Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory)
and
Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
Try path = #"c:\user\abcd\desktop\read.txt"
I haven't worked with C# for a while, but is it possible that the debugger is just showing you the escaped version of the string but the string itself is ok? Click on the magnifying glass next to the string in your debugger to show the text visualizer and see if it looks ok there.
I'm guessing that these are backslashes. This is the debugger's way of showing certain characters. A newline would show up as "\r\n", a tab as "\t", and a backslash as "\\". The string "a\b\c\d" will show up in the debugger as "a\\b\\c\\d" because that is what you have to type into code to get the actual string "a\b\c\d". When the debugger shows doubled backslashes, each \ is actually a single .
The debugger is just showing you the escaped version of the file. I note that one of your paths contains the string "user" while the other contains "users" is the pluralization the problem?

How do I see the hex values of a string in a VS2008 watch window?

I have a string in a watch window in VS2008 and want to see the hex representation of each character. If I right click there's a hexadecimal option but this doesn't appear to do anything. Anybody know how to view the string as a series of hex values?
Add your string as a watch, then edit the watch expression and append ".ToCharArray()" to view it as an array of chars. When you expand your watch you will see char code next to each individual char. Checking "Hexadecimal display" will show you hex codes for each character.
Default visualizer in VS (at least 2005) does not support this. However, apparently it isn't too much trouble to roll one's own visualizer: http://msdn.microsoft.com/en-us/library/ms379596.aspx (That's an old article from 2005 beta times, but I don't think the API changed much.)
Perhaps somebody somewhere even wrote one, but I haven't seen one yet.

Categories

Resources