String comparison: InvariantCultureIgnoreCase vs OrdinalIgnoreCase?
哪个代码更好:
1 | int index = fileName.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase); |
或
1 | int index = fileName.LastIndexOf(".", StringComparison.OrdinalIgnoreCase); |
这两种代码都不是最好的。他们做不同的事情,所以他们擅长不同的事情。
fxcop通常更喜欢
对于英语来说,差别很小。当你漫游到具有不同书面语言结构的语言时,这就成了一个问题。我没有足够的经验给你更多。
顺序排列情况
The StringComparer returned by the
OrdinalIgnoreCase property treats
the characters in the strings to
compare as if they were converted
to uppercase using the conventions
of the invariant culture, and then
performs a simple byte comparison
that is independent of language.
This is most appropriate when
comparing strings that are generated
programmatically or when comparing
case-insensitive resources such as
paths and filenames.
http://msdn.microsoft.com/en-us/library/system.stringcomparer.ordinalignorecase.aspx
不变文化特征酶
The StringComparer returned by the
InvariantCultureIgnoreCase property
compares strings in a linguistically
relevant manner that ignores case, but
it is not suitable for display in any
particular culture. Its major
application is to order strings in a
way that will be identical across
cultures.
http://msdn.microsoft.com/en-us/library/system.stringcomparer.invariantcultureignorecase.aspxThe invariant culture is the
CultureInfo object returned by the
InvariantCulture property.The InvariantCultureIgnoreCase
property actually returns an instance
of an anonymous class derived from the
StringComparer class.
如果你真的只想匹配点,那么
"序数"不使用文化和/或大小写规则,这些规则在类似于
你好像在做文件名比较,所以我想补充一下,