formatting strings with backslash - c#

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);

Related

Displaying phone format XAML/c#

In my windows phone project, I would like the user to enter his phone number in xxx-xxx-xxxx format. The country code it not required. I tried to implement regex, but i am not getting it right. I just want it to be displayed to the user as he enters it, nothing more, nothing less. This is what I have used
^\(\d{3}\) ?\d{3}( |-)?\d{4}$
But no matter what i put in, I always get this error (in this case 5) "Unrecognized escape sequence". I noticed, this is only with reference to the oblique. When I add a "" after it, the error goes away, but I do not get what I want. Is there a special way to input numbers in the textbox in than manner, on the XAML level?
Thanks in advance!
Put your regex inside verbatim string and also put the space, hyphen inside a group and make it as optional.
#"^\(\d{3}\)([- ]?)\d{3}\1\d{4}$"
DEMO
For testing your RegEx you can use this site: http://www.regexlib.com/RETester.aspx?AspxAutoDetectCookieSupport=1.
For your xxx-xxx-xxxx format I would use it:^\d{3}-?\d{3}-?\d{4}$

Why are some unintended symbols added to my string?

I wrote a console application which fetches strings from some fields in a Sharepoint list. Then I simply write the strings to console. This works fine for the most fields. There is one MultiLineTextField with RichText enabled where i had to remove all the html-tags, that causes this issue.
Even after all the tags are removed the strings seem to contain question marks which were never added to the string. The most weird thing about this is when I set a breakpoint and look into the string's value there are no question marks, but they suddenly appear on the console output.
The only thing I could think of was to Trim the string. Because sometimes they appear in front of the actual string sometimes they are at the and of it, but never in between.
So this is what I tried:
myString = myString.Trim();
myString = myString.Replace("?",string.Empty);
But this does not solve the issue. Besides this would not be a smart solution in case one of the strings would be supposed to contain question marks. For detailed code please see the link above.
Also Convert.ToBase64String(Encoding.UTF8.GetBytes(myString)) gives me the following output:
4oCLTWVobCwgRWllciwgV2Fzc2VyLCBIYWNrZmxlaXNjaCA=
There are probably some non-printing unicode (or possibly low ASCII) characters in the end of the string. The console has a different encoding, and will often render such as ?. Basically: use the indexer (yourString[n]) or yourString.ToCharArray() to investigate what is actually in the string aroung the location of the ?.
With the edit, we can see that the string has a zero-width space (decimal 8203) at the start:
Sounds like you're maybe having a problem with unicode characters. Chances are you're outputting the string as ASCII instead of Unicode. Take a look at this question as it sounds like you may be experiencing the same problem.

Why doesn't Path.GetFullName work in C#

I need to access the full path of a Resource file in my C# vs2010 project to use to create myType variable below. If I manually type the path as
#"C:\dir1\dir2\dir3\dir4\Source\dir5\dir6\Resources\myIcon.png"
the code works and accesses the icon. But if I use:
var result = Path.GetFullPath("myIcon.png")
The value in the variable result is the name of the correct directory except with two \ backslashes instead of one throughout i.e.
"C:\\dir1\\dir2\\dir3\\dir4\\Source\\dir5\\dir6\\myIcon.png"
. The problem is that I need to derive the result (file name variable) dynamically and I cant hard code it.
I don't know if I am supposed to manually get rid of the two \ and replace it with a single \ ? Or am I missing something.
var result = Path.GetFullPath("myIcon.png");
ToolboxItemWrapper myType = new ToolboxItemWrapper(
type,
#"C:\dir1\dir2\dir3\dir4\Source\dir5\ImAdmin\dir6\myIcon.png",
type.Name);
I am re-Editing my question. I have more than few problems, I am realizing after reading all the comments and the answers. So, I will mark the only answer as the answer but the one comment about being careful of using GetFullPath was also a valuable feedback. Thank you to all.
If you are seeing \\ in debugger, then it is simply debugger escaping the \ character so string appears the same way as if it were to appear as literal in the code. Clicking on the small magnifying glass will bring up dialog that shows true, human-readable value.

Spaces and backslashes in Visual Studio build events

I have an application that is supposed to aid my project in terms of pre- and post-build event handling. I'm using ndesk.options for command line argument parsing. Which gave me weird results when my project path contains spaces. I thought this was the fault of ndesk.options but I guess my own application is to blame. I call my application as a post-built event like so:
build.exe --in="$(ProjectDir)" --out="c:\out\"
A simple foreach over args[] displays the following:
--in=c:\my project" --out=c:\out"
What happened is that the last " in each parameter was treated as if it was escaped. Thus the trailing backslash was removed. And the whole thing is treated as a single argument.
Now I thought I was being smart by simply escaping the first " as well, like so:
build.exe --in=\"$(ProjectDir)" --out=\"c:\out\"
In that case the resulting args[] look like this:
--path="c:\my
project"
--out="c:\out"
The trailing backslash in the parameters is still swallowed and the first parameter is now split up.
Passing this args[] to ndesk.options will then yield wrong results.
How should the right command line look so that the correct elements end up in the correct args[] slots? Alternatively, how is one supposed to parse command line arguments like these with or without ndesk.options? Any suggestion is welcome.
Thanks in advance
Did you try to escape the last backslash?
build.exe --in="$(ProjectDir)\" --out="c:\out\\"
This works probably only, as long as the ProjectDir ends in \, which should be given.
This is just an idea, but I did not give it a try
EDIT:
I found a comment which suggests to leave out the trailing "
I actually used "." to solve this same problem:
build.exe --in="$(ProjectDir)." --out="c:\out\."
primarily because otherwise it might look like you are trying to escape the second quote...which you're not, you're escaping the final \ (which is hidden).
I also added a REM in the postbuild command describing why I did that.

C# Special Characters in String Crashing Program

I have a slight problem with a path:
"D:\\Music\\DJ Ti%C3%ABsto\\Tiesto\\Adagio For Strings (Spirit of London).mp3"
"D:\\Music\\Dj Tiësto\\Tiesto\\Adagio For Strings (Spirit of London).mp3"
Currently, when it sends that path to my Audio Library, it cannot open the path. (the reason for it crashing is trying to assign a -1 to a trackbar...but it's irrelevant).
So I'm wondering, is there anyway to prevent C# from switching special characters with %[code]? I've done a .Replace for "[" and "]", but I rather not have to look up every single special character, and add a line of code to prevent it. Is there anyway around this?
Call Uri.UnescapeDataString.
By the way, when putting paths in strings, you can put an # sign before the string to tell the compiler not to process escape codes, like this: #"D:\Music\DJ Tiësto\Tiesto\Adagio For Strings (Spirit of London).mp3". This way, you don't need to double up every backslash.

Categories