如何从字符串中删除加速符?

发布于 2024-09-18 17:32:18 字数 763 浏览 9 评论 0原文

我有一个 GUI 控件的标题,我想将其转换为简单的文本字符串。具体来说,我想删除加速器元字符。

例如(示例假设为 WinForms):

  • 去掉单个出现的元字符:&Yes 变为 Yes
  • 将两次出现的元字符转换为单个:Income &&费用 变为收入& Expense

我的代码将知道它是处理 Windows 窗体语法(加速器元字符为 &)还是 WPF(其中 _)。但是,这是在后端代码中,因此我不想依赖任何 WinForms 或 WPF 库函数 - 我需要使用核心(非 GUI)BCL 程序集来完成此操作。 (在这一点上,我认为任何适用于 WinForms 的东西对于 WPF 来说都是微不足道的修改。)

我可以想到几种方法来做到这一点,但不清楚哪种方法最简单。

实现“如果是单字符则删除元字符,如果是双字符则去双字符”的最简单方法是什么?

更新: 我曾以为 WinForms 和 WPF 处理这些问题的方式基本相同,但事实证明它们并非如此。 WinForms 将删除字符串末尾的一个单独元字符(Foo& 变为 Foo),但 WPF 不会(Foo_ 仍为 Foo_)。解决这两个问题的答案会加分。

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: &Yes becomes Yes
  • Convert double occurrences to single: Income && Expense becomes Income & 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2024-09-25 17:32:18

我已经编辑(删除)了我之前的答案。我认为最简单的方法是这个正则表达式:

string input = "s&trings && stuf&f &";
input = Regex.Replace(input, "&(.)", "$1");

它可以正确处理重复的&符号以及&符号是最后一个字符的情况。

根据提供的其他信息进行编辑:

因此 WinForms 表达式将为 "&(.?)",WPF 表达式将为 "_(.)"。您要求解决这两种情况的解决方案,但我不确定您在问什么。您最初的问题是说代码知道它是处理 WPF 格式还是 WinForms 格式。所以我会设想一种方法:

string StripAccelerators(string s, bool isWinForms)
{
    string pat = (isWinForms) ? "&(.?)" : "_(.)";
    return Regex.Replace(s, pat, "$1");
}

并且,是的,我意识到在界面中使用 Boolean 标志并不理想。理想情况下,您应该使用枚举类型,或者可能有两个单独的方法。

我认为您不希望使用一个正则表达式来执行这两项操作。这是可能的,但最终您会从 WinForms 字符串中删除下划线,或从 WPF 字符串中删除与号。

I've edited (removed) my previous answer. I think the simplest way would be this regular expression:

string input = "s&trings && stuf&f &";
input = Regex.Replace(input, "&(.)", "$1");

That correctly handles repeated ampersands as well as the case where the ampersand is the last character.

EDIT, based on additional provided information:

So the WinForms expression would be "&(.?)", and the WPF expression would be "_(.)". You ask for a solution that addresses both cases, but I'm not sure what you're asking. Your original question said that the code knows whether it's processing WPF format or WinForms format. So I would envision a method:

string StripAccelerators(string s, bool isWinForms)
{
    string pat = (isWinForms) ? "&(.?)" : "_(.)";
    return Regex.Replace(s, pat, "$1");
}

And, yes, I realize that using a Boolean flag in the interface is less than ideal. Ideally, you'd use an enumerated type, or perhaps have two separate methods.

I don't think you want to have a single regular expression that will perform both. It's possible, but then you'll end up removing underlines from WinForms strings, or ampersands from WPF strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文