Dotnet Core - 收到有关"字符串"的警告。替换(字符串,字符串?)"说使用"字符串。Replace(string, string?, System.StringComparison)'



我在"替换"上收到以下警告

> 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:UserssimonOneDriveDocuments1.0
> - AURELIA1.0 - JobsLedgerSPA -ASPNET CORE 3.1JobsLedger.APIControllerServicesCommonOrderAndFIlterHelpersODataProcessQuery.cs    38  Active

我不知道如何重新配置以下内容以考虑"系统字符串比较":

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

每一行都抛出了一个警告。

我正在使用VS2019,这些警告来自Roslyn编译器。我想摆脱警告..我如何重写它以考虑到替换的"系统字符串比较"部分?

只是...告诉它你想要什么比较类型;例如,对于序号忽略大小写替换:

.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进行硬编码系统式替换;基于区域性的替换对于面向用户的替换更为典型(想想:Ctrl+F(。作为旁注,使用string.Empty比更清晰(IMO(""真的没有任何好处。

最新更新