Dotnet Core – Getting a warning on 'string.Replace(string, string?)' saying use 'string.Replace(string, string?, System.StringComparison)'

huangapple go评论99阅读模式
英文:

Dotnet Core - Getting a warning on 'string.Replace(string, string?)' saying use 'string.Replace(string, string?, System.StringComparison)'

问题

我收到了关于“Replace”的以下警告:

严重性 代码 描述 项目 文件 行 抑制状态
警告 CA1307 '字符串.Replace(字符串,字符串?)'的行为可能
根据当前用户的区域设置而异。在
'JobsLedger.API.ControllerServices.Common.OrderAndFIlterHelpers.ODataProcessQuery.ProcessQuery(string)'中替换此调用
为调用'string.Replace(string, string?,
System.StringComparison)'。 JobsLedger.API C:\Users\simon\OneDrive\Documents\1.0

  • AURELIA\1.0 - JobsLedgerSPA -ASPNET CORE 3.1\JobsLedger.API\ControllerServices\Common\OrderAndFIlterHelpers\ODataProcessQuery.cs 38 活动

我不知道如何重新配置以下内容以考虑'System.StringComparison':

.Replace("and", "&&")
.Replace("substringof", string.Empty)
.Replace("(", string.Empty)
.Replace(")", string.Empty)
.Replace("'", string.Empty)
.Replace(" ", string.Empty)
.Replace("eq", ",")

每一行都引发了一个警告。

我正在使用VS2019,这些警告来自Roslyn编译器。我想摆脱这些警告...我该如何重写以考虑Replace中的'System.StringComparison'部分?

英文:

I m getting the following warning on "Replace"

> Severity	Code	Description	Project	File	Line	Suppression State
> Warning	CA1307	The behavior of 'string.Replace(string, string?)' could
> vary based on the current user's locale settings. Replace this call in
> 'JobsLedger.API.ControllerServices.Common.OrderAndFIlterHelpers.ODataProcessQuery.ProcessQuery(string)'
> with a call to 'string.Replace(string, string?,
> System.StringComparison)'.	JobsLedger.API	C:\Users\simon\OneDrive\Documents.0
> - AURELIA.0 - JobsLedgerSPA -ASPNET CORE 3.1\JobsLedger.API\ControllerServices\Common\OrderAndFIlterHelpers\ODataProcessQuery.cs	38	Active

I dont know how to reconfigure the following to take into account 'System.StringComparison':

							.Replace("and", "&")
							.Replace("substringof", string.Empty)
							.Replace("(", string.Empty)
							.Replace(")", string.Empty)
							.Replace("'", string.Empty)
							.Replace(" ", string.Empty)
							.Replace("eq", ",")

Each line has thrown up a warning..

I am using VS2019 and these warnings are coming from the Roslyn compiler. I would like to get rid of the warnings.. how do I rewrite this to take into account the 'System.StringComparison' part of the Replace?

答案1

得分: 4

只需告诉它你想要的比较类型;例如,对于忽略大小写的有序替换:

    .Replace("and", "&", StringComparison.OrdinalIgnoreCase)
    .Replace("substringof", string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace("(", string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace(")", string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace("'", string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace("eq", ",", StringComparison.OrdinalIgnoreCase);

有关每个选项的描述,请查看StringComparison。一般来说,对于硬编码的类似系统替换,你不应该使用CurrentCulture / CurrentCultureIgnoreCase;基于文化的替换更适用于面向用户的替换(比如:<kbd>ctrl</kbd>+<kbd>f</kbd>)。另外,使用string.Empty 和更清晰(我个人认为)的"" 没有明显的好处。

英文:

just... tell it what comparison type you want; for example, for an ordinal-ignore-case replace:

    .Replace(&quot;and&quot;, &quot;&amp;&quot;, StringComparison.OrdinalIgnoreCase)
    .Replace(&quot;substringof&quot;, string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace(&quot;(&quot;, string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace(&quot;)&quot;, string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace(&quot;&#39;&quot;, string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace(&quot; &quot;, string.Empty, StringComparison.OrdinalIgnoreCase)
    .Replace(&quot;eq&quot;, &quot;,&quot;, StringComparison.OrdinalIgnoreCase);

For descriptions of what each option does, look at StringComparison. Generally speaking, you should not use CurrentCulture / CurrentCultureIgnoreCase for hard-coded system-like replacement; culture-based replace is more typical for user-facing replacements (think: <kbd>ctrl</kbd>+<kbd>f</kbd>). As a side-note, there's really no benefit over using string.Empty over the clearer (IMO) &quot;&quot;.

huangapple
  • 本文由 发表于 2020年1月6日 21:49:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定