C# 全球化

内容纲要

如果写 C# 时,使用了分析器,很可能会提示

string.ToString() 的行为可能因当前用户的区域设置而异

这里针对某些字符/字符串转换时出现的问题,做个列表。

IFormatProvider

这个接口用于实现字符串如何格式化转为数值类型。一般来说6660.666 这样的数没问题,但是使用数学符号等情况、全球化字符差异,可能会出错。
IFormatProvider 正是为了解决这些问题。

Convert.ToInt32string.ToString()将字符串/Object转为值类型时,会提示 需要 IFormatProvider

而 char 、byte 等则不会出现此现象。因此一方面考虑尽量不使用字符串。

只有以下两个重载会被提示需要 IFormatProvider,需要自己实现一个 IFormatProvider 接口。

ToInt32(Object, IFormatProvider)
ToInt32(String, IFormatProvider)
使用指定的区域性特定格式信息,将指定对象的值转换为 32 位带符号整数。

可以查看
https://docs.microsoft.com/zh-cn/dotnet/api/system.convert.toint32?view=netframework-4.8#System_Convert_ToInt32_System_Object_System_IFormatProvider_

NumberFormatInfo 提供用于对数字值进行格式设置和分析的区域性特定信息。
这个类型对简化字符串转换操作非常有帮助。
可以通过特定规则将各种字符串存放的各种数字形式取出来。
请查看
https://docs.microsoft.com/zh-cn/dotnet/api/system.globalization.numberformatinfo?view=netframework-4.8#properties

System.Globalization.CultureInfo.CurrentCulture

提供有关特定区域性(对于非托管代码开发,则称为“区域设置”)的信息。 这些信息包括区域性的名称、书写系统、使用的日历、字符串的排序顺序以及对日期和数字的格式化设置。

ToLwer() 等字符串操作时,需要使用到 CultureInfo
可以查看
https://docs.microsoft.com/zh-cn/dotnet/api/system.globalization.cultureinfo?view=netcore-3.1

一般我们可以这样

.ToLower(System.Globalization.CultureInfo.CurrentCulture)

StringComparison

将指定的 StringComparison 实例转换为 StringComparer 实例。
一般在字符串对比查找时出现。
不同方式查找对比字符串,性能有所差异,具体可以查看
https://blog.csdn.net/li5852258/article/details/70326763

一般来说,我们这样使用就行了

.Contains("a",StringComparison.CurrentCulture)
点赞
  1. I am only writing to make you understand what a terrific experience our princess undergone viewing yuor web blog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal teaching style to make the others without hassle thoroughly grasp specified tricky topics. You undoubtedly surpassed visitors' expected results. Thanks for showing those valuable, trustworthy, revealing not to mention unique tips on your topic to Sandra.

  2. I simply wished to thank you very much yet again. I do not know what I would have worked on without those tricks shown by you relating to this topic. This was the troublesome situation in my view, nevertheless witnessing your well-written way you treated the issue took me to weep with joy. Now i am thankful for your work as well as wish you recognize what an amazing job your are undertaking training the rest with the aid of your websites. Probably you haven't met any of us.

  3. I'm also commenting to let you be aware of of the beneficial discovery my child encountered checking the blog. She came to understand a lot of details, including how it is like to have a marvelous coaching character to get certain people completely comprehend a variety of complex issues. You actually did more than her desires. I appreciate you for coming up with such essential, dependable, edifying and cool guidance on that topic to Ethel.

  4. buy kamagra说道:

    I wanted to put you the very little remark to be able to thank you the moment again over the remarkable tactics you have provided on this page. This has been simply surprisingly open-handed with you to offer unhampered precisely what most of us would've offered for sale for an ebook to help make some money on their own, particularly seeing that you might have tried it in case you desired. Those strategies in addition worked to become good way to know that some people have the identical keenness really like my personal own to realize much more when it comes to this matter. I believe there are several more pleasant sessions up front for people who scan your site.

  5. whuanle说道:

    需要注意的是,如果使用了 EFCore 等表达式树翻译,使用全球化可能会导致出错!

发表评论

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!