Verbatim strings and Visual Studio - c#

Is there a way to get Visual Studio to display strings as verbatim strings (prefixed with '#')? I'd like to easily cut strings containing file paths from Visual Studio into Explorer or other apps.
Clarification: when VS displays a string in the auto, watch, immediate, etc. window, I'd like it to be formatted as as verbatim string so that I can simple copy it for use elsewhere.

You can click on the magnifier in VS 2008 debugger variable display and select "Text Visualizer" which will give you the text in an unmassaged format.

You may need to clarify your question. My first thought was that you just need to prefix the string with '#' to make them verbatim, but you already know that.
string s = #"c:\my folder\";
What exactly are you trying to do with the string?

Here's a discussion on how to actually build verbatim-ized strings:
Can I convert a C# string value to a string literal.
Plugging this in a debug vizualiser is a different story...

Related

Why does Resharper give me a 'Localizable Interpolated String' hint here?

I'm using VS 2015 with Resharper Ultimate for a Win Forms application and was doing something as simple as setting the text property of a button when I saw the blue squiggly line below the string.
Clicking on it converts the statement to the one I've retyped just below the problematic statement and there's no blue squiggly. Note that it's got the verbatim and the interpolation operators.
Also, setting the same string to another string with just interpolation seems to work fine. There's no characters to escape in this string. What's happening here?
As for verbatim string: Untick "ReSharper | Options | Code Editing | C# | Localization | Don't analyse verbatim string" checkbox and then ReSharper will show you such kind of suggestion for verbatim string as well.
As for why it shows squiggle for WinForms property but does not for a string: please refer to a webhelp article about "Localizable inspector" property. In case you would like to get the suggestion for all strings as well, you need to change "Localizable inspector" value to "Pessimistic"
You are setting a UI element's text to a string, and UI should be subject to localization.
On the other hand, interpolation is equivalent to just calling string.Format with default format provider.
It is assumed that UI elements will be populated from resources and that they will be formatted using specific culture. That is why ReSharper is sensitive to this particular use of string interpolation.
What you said about setting a different string to the interpolated string and then assigning that to the UI element, you are just playing tricks on ReSharper. It was not able to infer that you have assigned the UI element to the string which is not localized, but the problem is still there.

Appended extra Escape character in string in c# [duplicate]

var phone = #"^\+(?:[0-9] ?){6,14}[0-9]$";
phone will then equal ^\\+(?:[0-9] ?){6,14}[0-9]$
I thought (and the examples I found seem to show) the # character meant to leave my string how I have it. Why is it doubling the \ and how do I stop it?
The visual studio debugger will show it as if it were doubled, since in C# a \ would precede an escape sequence. Don't worry - your string is unchanged.
It only looks like it's doubled in the debug inspectors.
Note that the strings shown in the inspectors don't start with # - they are showing how you would have to write the string if you were to do it without the #. The two forms are equivalent.
If you're really worried about the contents of the string, output it in a console app.
To reiterate in another way, the comparison
var equal = #"^\+(?:[0-9] ?){6,14}[0-9]$" == "^\\+(?:[0-9] ?){6,14}[0-9]$"
will always be true. As would,
var equal = #"\" == "\\";
If you examine the variables using the Text Visualizer, you will be shown the plain unescaped string, as it was when you declared it verbatim.

String contains quotes in Visual Studio debugger but string.contains returns false

I have really weird behavior I have a string like in the picture:
You can see the string s have quotes in the beginning and in the end but the function s.Contains("\"")
returns false. Someone help please what am I missing?
As everyone has already stated in the comments, the Start and End quotes are added by Visual Studio when displaying a string type.
If you were to click the magnifer glass you will bring up the Text Visualizer. In the visualizer you will notice that the quotes are not visibile.
Update: (per Alex suggestion) Just to add here is the display of a string that contains quotes with the helper and the Text Visualizer.
-Text Visualizer
Immediate Window
Output window
Cheers.

Converting combining diacritics to simple utf

I have a problem when inserting a string to database due to some encoding issues.
String source is a external rss feed.
In web browser it looks ok. Even in debugger the text appears to be ok.
If I copy the strong to notedpad, the result is also ok.
But in notepad++ was possible to see that string is using combining characters.
If changing to ansii, both combined appears.
e.g.
á is displayed as a´
(In notepad++ is is like having two chars, on over the other. I even can select ... half of the char)
I googled a lot and tried very different approach to this problem.
I really want to find a clever way of convert string with combining diacritics to simple utf8 database compatible ones.
Any help?
Thank you so much!
This should work for you
output.Normalize(NormalizationForm.FormC)
This little test gave 3, 2, 3. The middle string is correctly combining A and it's diacritic into a single UTF-8 character
Console.WriteLine(Encoding.UTF8.GetByteCount(("A\u0302")));
Console.WriteLine(Encoding.UTF8.GetByteCount(("A\u0302").Normalize(NormalizationForm.FormC)));
Console.WriteLine(Encoding.UTF8.GetByteCount(("T\u0302").Normalize(NormalizationForm.FormC)));
My Mac can solve this running the following Command in Terminal:
iconv -f utf-8-mac -t utf-8 inputfile >outputfile

formatting strings with backslash

I'm a newbie to c# so hopefully this one isn't too hard for a few of you.
I'm trying to build a string that has a \ in it and I am having difficulty getting just one backslash to show up even though I am adding additional escape chars or ignoring them all together. Can someone show me what I am doing wrong?
What I want my string to look like:
"10.20.14.103\sql08"
What I've tried so far:
I added an additional character to make the compiler happy but it did not escape it.
ip = string.Format("{0}\\\\{1}", ip, instancename); // output has 2 \'s
I told it to ignore escapes, it decided to ignore me instead
string temp = #"192.168.1.200\sql08"; // output has 2 \'s
Can someone help me make sense of this? (The richtext editor here seems to do a better job with it than VS2010 is doing, lol)
I'm guessing you're getting confused by the debugger.
If you hover your mouse over a local variable in VS, strings will be escaped so a single \ will display as \\.
To see what your string really is, output it somewhere for display (e.g., to the console) or hover your mouse on the variable, click on the arrow next to the little magnifying glass that appears, and select "Text Visualizer."
If you're looking at these strings in the debugger (i.e., by hovering the mouse over the variable or using a watch), the debugger adds escape characters to the display string so that it's a valid string expression. If you want to view the string verbatim in this fashion, click on the magnifying glass on the right side of the tooltip or watch entry with the string in it.
I'm guessing you're looking at the values in the debugger and seeing that they have two slashes.
That's normal. The debugger will show two slashes even though the actual string representation will only have one. Just another hump to get over when getting used to the debugger.
Be assured that when you actually use your strings, they will still only have a single slash (using either of your methods).
string requiredString = string.Format(#"{0}\\{1}",str1,str2);

Categories