C# string.indexof(string)返回1!!!

下面的代码第一次indexof返回0,第二次indexof返回1。

const string softHyphenPlusHyphen = "\xAD\x2D";
Console.WriteLine("softHyphenPlusHyphen.IndexOf(softHyphenPlusHyphen,
StringComparison.Ordinal): " +
softHyphenPlusHyphen.IndexOf(softHyphenPlusHyphen,
StringComparison.Ordinal));
Console.WriteLine("softHyphenPlusHyphen.IndexOf(softHyphenPlusHyphen): " + softHyphenPlusHyphen.IndexOf(softHyphenPlusHyphen));


Best Practices for Using Strings in the .NET Framework讲了如何在.net中正确的使用字符串。摘要如下:

When you develop with the .NET Framework, follow these simple recommendations when you use strings:

    Use overloads that explicitly specify the string comparison rules for string operations. Typically, this involves calling a method overload that has a parameter of type StringComparison.

    Use StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.

    Use comparisons with StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for better performance.

    Use string operations that are based on StringComparison.CurrentCulture when you display output to the user.

    Use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase values instead of string operations based on CultureInfo.InvariantCulture when the comparison is linguistically irrelevant (symbolic, for example).

    Use the String.ToUpperInvariant method instead of the String.ToLowerInvariant method when you normalize strings for comparison.

    Use an overload of the String.Equals method to test whether two strings are equal.

    Use the String.Compare and String.CompareTo methods to sort strings, not to check for equality.

    Use culture-sensitive formatting to display non-string data, such as numbers and dates, in a user interface. Use formatting with the invariant culture to persist non-string data in string form.

Avoid the following practices when you use strings:

    Do not use overloads that do not explicitly or implicitly specify the string comparison rules for string operations.

    Do not use string operations based on StringComparison.InvariantCulture in most cases. One of the few exceptions is when you are persisting linguistically meaningful but culturally agnostic data.

    Do not use an overload of the String.Compare or CompareTo method and test for a return value of zero to determine whether two strings are equal.

    Do not use culture-sensitive formatting to persist numeric data or date and time data in string form.

下表是如何选择StringComparison:

201207251534237908.png

转载于:https://www.cnblogs.com/fresky/archive/2012/07/25/2608415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值