c# - Dotnet Core - 在 'string.Replace(string, string?)' 上收到警告说使用 'string.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:\Users\simon\OneDrive\Documents\1.0
> - AURELIA\1.0 - JobsLedgerSPA -ASPNET CORE 3.1\JobsLedger.API\ControllerServices\Common\OrderAndFIlterHelpers\ODataProcessQuery.cs 38 Active
我不知道如何重新配置以下内容以考虑“System.StringComparison”:
.Replace("and", "&")
.Replace("substringof", string.Empty)
.Replace("(", string.Empty)
.Replace(")", string.Empty)
.Replace("'", string.Empty)
.Replace(" ", string.Empty)
.Replace("eq", ",")
每一行都抛出了一个警告..
我正在使用 VS2019,这些警告来自 Roslyn 编译器。我想摆脱警告..如何重写它以考虑替换的“System.StringComparison”部分?
解决方案
只是...告诉它你想要什么比较类型;例如,对于序数忽略大小写替换:
.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
clearer (IMO)确实没有任何好处""
。
推荐阅读
- c++ - VSCode - 调试 C++ 时无法将输入输入控制台
- amazon-web-services - aws emr s3-dist-cp MapReduce 作业在 CopyFilesReducer.cleanup 上失败
- swift - 选择一个 CoreData 对象并将其作为绑定传递
- r - 使用R循环更改名称的多个excel文件
- python - tensorflow conv1d,在这段代码中为什么我不能使用 2 或 3 的 kernel_size
- vba - VBA Outlook 检测新电子邮件
- python - 如何在 pandas 中对 apply 函数进行parrellize?
- c# - 从自包含的 .net 核心控制台应用程序在 Mac 上创建 .app 包
- nginx - Websocket 配置在 ubuntu 20.04 (AWS EC2) 上的 nginx 1.18.0 中不起作用
- html - 水平滚动不适用于 slimscroll