Reputation: 596
I have the following problem with String.Replace:
string str = "0˄0";
str = str.Replace("˄", "&&");
when I print out str, it will be "0&0" instead of "0&&0". To get "0&&0", I have to write
str = str.Replace("˄", "&&&&");
Why is that?
Upvotes: 0
Views: 600
Reputation: 153
You may want to check out this post: http://moiashvin-tech.blogspot.com/2008/05/escape-ampersand-character-in-c.html
Labels are handled differently in WinForms. You should be able to do as the post suggests and set the UseMnemonic property to false, see if that works.
Upvotes: 3
Reputation: 39500
The answer is in the first comment - '&' is a special character for WinForms (and the underlying Win32 API) which is/was used to indicate a shortcut character for menu/dialog items.
'&' in strings means nothing special to C#, but if you want to put it on a form/dialog label, then you need to escape the '&' by adding another one in front.
Personally, I would get the string how I really wanted it in my 'business logic' first, then escape the '&' characters as part of displaying the string on the form.
Upvotes: 1
Reputation: 50825
&
is a special character in WinForms, used to indicate keyboard shortcuts. Using &&
is how WinForms escapes the &
symbol.
So, to display it in WinForms you are necessarily going to have to place two &
characters in your string as you have here:
str = str.Replace("˄", "&&&&");
This is strictly a WinForms "thing" and has nothing to do with a C# or .NET string escaping specifically. It has been this way at least as far back as Visual Basic 4 - probably before then.
Upvotes: 6