哪种代码更好:
int index = fileName.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase);
or
int index = fileName.LastIndexOf(".", StringComparison.OrdinalIgnoreCase);
哪种代码更好:
int index = fileName.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase);
or
int index = fileName.LastIndexOf(".", StringComparison.OrdinalIgnoreCase);
FXCop通常更喜欢OrdinalIgnoreCase。但是你的要求可能会有所不同。
对于英语来说,差别很小。当你接触到具有不同书面语言结构的语言时,这就成了一个问题。我没有足够的经验给你更多。
OrdinalIgnoreCase
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
InvariantCultureIgnoreCase
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.aspx The 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.
这两种代码都不是更好的。他们做不同的事情,所以他们擅长不同的事情。
InvariantCultureIgnoreCase使用基于英语的比较规则,但没有任何区域差异。这对于考虑到一些语言方面的中性比较来说是很好的。
OrdinalIgnoreCase比较没有文化方面的字符代码。这适用于精确的比较,如登录名,但不适用于对具有不寻常字符的字符串进行排序,如é或ö。这也更快,因为在比较之前不需要应用额外的规则。
你似乎在做文件名比较,所以我只是补充,OrdinalIgnoreCase是最接近NTFS做的(它不完全相同,但它比InvariantCultureIgnoreCase更接近)
如果你只想匹配点,那么StringComparison。序数是最快的,因为没有区别。
“Ordinal”不使用区域性和/或大小写规则,这些规则无论如何都不适用于符号,如..