可以创建区分大小写的字符串类? [英] Possible to create case insensitive string class?

查看:216
本文介绍了可以创建区分大小写的字符串类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将需要什么来创建其他行为完全像一个字符串,不区分大小写字符串类型?

What would be required to create a case-insensitive string type that otherwise behaves exactly like a string?

我从来没有听说过任何人作出这样的区分大小写的字符串类型,它显然不是框架的一部分,但它似乎像它可能是非常有用的。是,SQL但不区分大小写的比较默认的事实,就是一个很大的情况下。所以,我想这不是没有可能的,否则有一个真的很好的理由,为什么没有人做它,我不知道。

I've never heard of anyone making a case insensitive string type like this and it's obviously not part of the framework, but it seems like it could be very useful. The fact that SQL does case insensitive comparisons by default is a great case in point. So I'm thinking it's either not possible, or else there's a really good reason why no one does it that I'm not aware of.

我知道这将需要使用隐含运营商分配了,你就必须重写相等操作符。而对于覆盖GetHash code(),我想你可以只返回TOLOWER()。GetHash code()。

I know it would require using an implicit operator for assignment, and you would have to override the equals operator. And for overriding GetHashCode(), I'm thinking you could just return ToLower().GetHashCode().

我在想什么?

推荐答案

它不会表现得的完全像一个字符串的。该字符串类型是特殊的,被烤成的语言规范。 C#中的字符串显示出特殊行为,如

It wouldn't behave "exactly like a string". The string type is special and is baked into the language spec. C# strings exhibit special behavior, such as

  • 是一个引用的类型,被按值传递。引用类型的...通常通过好......参考。

  • being a reference type, that gets passed by value. Reference types are normally passed by...well...reference.

实习的默认。这意味着,只有永远给定的字符串的单个实例。在创建只是一个单一字符串的下列code结果: A B 和<$ C $ ç> C 都指向字符串完全相同的情况下快捷。这意味着, Object.ReferenceEquals()比较任意两个的时候是正确的:

are interned by default. That means that there is only ever a single instance of a given string. The following code results in the creation of just a single string: a, b and c all point to exactly the same instance of the string quick. That means that Object.ReferenceEquals() is true when comparing any two:

string a = "The quick brown dog...".Substring(4,5) ;
string b = new string(new char[]{'q','u','i','c','k'});
string c = new StringBuilder().
           .Append('q')
           .Append('u')
           .Append('i')
           .Append('c')
           .Append('k')
           .ToString()
           ;

[编辑要注意:虽然有人可能会认为这应该是可能的,稍微摆弄周围表明,人们不能真正创建 CompareInfo的自定义实现/亚型,因为它没有公共构造和它的默认构造函数是内部。多在回答这个问题:<一href="http://stackoverflow.com/questions/11138387/globally-set-string-compare-compareinfo-compare-to-ordinal">Globally设置的String.Compare / CompareInfo.Compare为序

[edited to note: while one might think that this should be possible, a little fiddling around suggests that one can't actually create a custom implementation/subtype of CompareInfo as it has no public constructors and its default constructor is internal. More in the answers to this question: Globally set String.Compare/ CompareInfo.Compare to Ordinal

哎呀...]

你是什么可以做是这样的:

比较字符串使用当前区域性的整理/比较规则进行。你的应用程序创建一个自定义的文化,说的,美国文化的一个使用你所需要的整理/比较规则的副本。设置为当前文化和Bob's-揭掉叔叔。

String comparisons are done using the current culture's collation/comparison rules. Create a custom culture for your app, say, a copy of the the US culture that uses the collation/comparison rules you need. Set that as the current culture and Bob's-yer-uncle.

您还是会得到编译/ ReSharper的抱怨道,因为你在做字符串比较不指定所需要的比较语义,但你的code将是干净的。

You'll still get compiler/ReSharper whines because you're doing string comparisons without specifying the desired comparison semantics, but your code will be clean.

有关详细信息,请参阅

  • https://msdn.microsoft.com/en-us/library/kzwcbskc(v=vs.90).aspx
  • https://msdn.microsoft.com/en-us/library/se513yha(v=vs.100).aspx

这篇关于可以创建区分大小写的字符串类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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