string.Equals(c#)的culture参数何时实际产生影响的示例?

前端之家收集整理的这篇文章主要介绍了string.Equals(c#)的culture参数何时实际产生影响的示例?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不完全理解string.Equals的第二个参数,这是因为我找不到任何实际会产生影响的例子.例如,这里给出的示例是相同的,无论第二个参数的值如何(除了IgnoreCase):
http://msdn.microsoft.com/en-us/library/c64xh8f9.aspx

我只是在讨论StringComparison.CurrentCulture,InvariantCulture或Ordinal的值.
我可以理解这些与IgnoreCase等价物之间的区别.

解决方法

This MSDN页面(在.NET Framework中使用字符串的最佳实践)有很多关于使用字符串的信息,以下示例取自它:
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4.  
  5. public class Example
  6. {
  7. public static void Main()
  8. {
  9. string[] values= { "able","ångström","apple","Æble","Windows","Visual Studio" };
  10. Array.Sort(values);
  11. DisplayArray(values);
  12.  
  13. // Change culture to Swedish (Sweden).
  14. string originalCulture = CultureInfo.CurrentCulture.Name;
  15. Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
  16. Array.Sort(values);
  17. DisplayArray(values);
  18.  
  19. // Restore the original culture.
  20. Thread.CurrentThread.CurrentCulture = new CultureInfo(originalCulture);
  21. }
  22.  
  23. private static void DisplayArray(string[] values)
  24. {
  25. Console.WriteLine("Sorting using the {0} culture:",CultureInfo.CurrentCulture.Name);
  26. foreach (string value in values)
  27. Console.WriteLine(" {0}",value);
  28.  
  29. Console.WriteLine();
  30. }
  31. }
  32. // The example displays the following output:
  33. // Sorting using the en-US culture:
  34. // able
  35. // Æble
  36. // ångström
  37. // apple
  38. // Visual Studio
  39. // Windows
  40. //
  41. // Sorting using the sv-SE culture:
  42. // able
  43. // Æble
  44. // apple
  45. // Windows
  46. // Visual Studio
  47. // ångström

猜你在找的C#相关文章