字符串比较,.NET和不间断空格 [英] String Comparison, .NET and non breaking space

查看:90
本文介绍了字符串比较,.NET和不间断空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#编写的应用程序,它可以进行很多字符串比较。从各种来源(包括用户输入)提取字符串,然后进行比较。但是,在将空格 32与不间断空格 160进行比较时,我遇到了问题。对于用户来说,它们看起来相同,因此他们期望匹配。但是当应用程序进行比较时,没有匹配项。

I have an app written in C# that does a lot of string comparison. The strings are pulled in from a variety of sources (including user input) and are then compared. However I'm running into problems when comparing space '32' to non-breaking space '160'. To the user they look the same and so they expect a match. But when the app does the compare, there is no match.

进行此操作的最佳方法是什么?我是否必须去做字符串比较并手动将不间断空格标准化为空格的代码的所有部分? .NET是否提供任何帮助呢? (我尝试了所有的比较选项,但似乎都无济于事。)

What is the best way to go about this? Am I going to have to go to all parts of the code that do a string compare and manually normalize non-breaking spaces to spaces? Does .NET offer anything to help with that? (I've tried all the compare options but none seem to help.)

有人建议我在收到时对字符串进行规范化,然后简单地让字符串compare方法比较规范化的字符串。我不确定这样做是否简单,因为首先是什么是规范化字符串。我也将其标准化吗?当然,现在我可以将不间断空格转换为间断空格。但是还能显示什么呢?这些规则中可能有很多吗?他们甚至可能会发生冲突。 (在一种情况下,我想使用规则,在另一种情况下,我不想使用。)

It has been suggested that I normalize the strings upon receipt and then let the string compare method simply compare the normalized strings. I'm not sure it would be straight-forward to do that because what is a normalized string in the first place. What do I normalize it too? Sure, for now I can convert non-breaking spaces to breaking spaces. But what else can show up? Can there potentially be very many of these rules? Might they even be conflicting. (In one case I want to use a rule and in another I don't.)

推荐答案

如果是我,我会当我将它们拉入时会规范化字符串;可能带有string.Replace()。这样,您就无需在其他任何地方更改比较。

If it were me, I would 'normalize' the strings as I 'pulled them in'; probably with a string.Replace(). Then you won't need to change your comparisons anywhere else.

编辑:马克,这很艰难。什么是规范化字符串取决于您或您的客户。我遇到过类似的情况,客户要求使用以下字符串:

Edit: Mark, that's a tough one. Its really up to you, or you clients, as to what is a 'normalized' string. I've been in a similar situation where the customer demanded that strings like:


I have 4 apples.
I have four apples.

实际上是相等的。您可能需要针对不同情况的单独的规范化器。无论哪种方式,我仍然会在检索原始字符串时进行归一化。

were actually equal. You may need separate normalizers for different situations. Either way, I would still do the normalization upon retrieval of the original strings.

这篇关于字符串比较,.NET和不间断空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆