I have a caption for a GUI control, and I want to convert it to a simple text string. Specifically, I want to remove the accelerator metacharacters.
For example (examples assume WinForms):
- Strip off single occurrences of the metacharacter:
&YesbecomesYes - Convert double occurrences to single:
Income && ExpensebecomesIncome & Expense
My code will know whether it's dealing with the Windows Forms syntax (where the accelerator metacharacter is &) or WPF (where it's _). However, this is in back-end code, so I don't want to take a dependency on any WinForms or WPF library functions -- I need to do this using the core (non-GUI) BCL assemblies. (And at that point, I figure that anything that works for WinForms would be trivial to modify for WPF.)
I can think of several ways to do this, but it's not clear which is simplest.
What's the simplest way to implement this "remove metacharacters if single, de-double if doubled"?
Update: I had assumed that WinForms and WPF both handled these basically the same, but it turns out they don't. WinForms will strip a lone metacharacter at the end of the string (Foo& becomes Foo), but WPF will not (Foo_ remains Foo_). Bonus points for an answer that addresses both.