gpt4 book ai didi

c# - 如何在 C# (CA1307) 中使用 StringComparison?

转载 作者:行者123 更新时间:2023-12-02 00:46:00 27 4
gpt4 key购买 nike

我的目标是省略author_namepipelineauthor_emailpipeline@gmail.com 如果项目名称以ej2Ej2开头。

我有以下代码。此代码完全符合我的预期。

if ((author_name != "pipeline" && author_email != "pipeline@gmail.com") && 
(projectName.StartsWith("ej2") == false || projectName.StartsWith("Ej2") == false))
{
// some operations
}

我的 FxCop 说要使用上述方法违反以下规定。

Severity Code Description Project File Line Suppression State Warning CA1307 Because the behavior of 'string.StartsWith(string)' could vary based on the current user's locale settings, replace this call in 'GetProjectDetailsByApi.DevelopmentBranchStatus(List)' with a call to 'string.StartsWith(string, StringComparison)'. If the result of 'string.StartsWith(string, StringComparison)' will be displayed to the user, such as when sorting a list of items for display in a list box, specify 'StringComparison.CurrentCulture' or 'StringComparison.CurrentCultureIgnoreCase' as the 'StringComparison' parameter. If comparing case-insensitive identifiers, such as file paths, environment variables, or registry keys and values, specify 'StringComparison.OrdinalIgnoreCase'. Otherwise, if comparing case-sensitive identifiers, specify 'StringComparison.Ordinal'.

所以我像下面这样更改了我的代码。但是我只有

if (author_name.Equals("pipeline", StringComparison.OrdinalIgnoreCase) == false
&& author_email.Equals("pipeline@gmail.com", StringComparison.OrdinalIgnoreCase) == false
&& projectName.StartsWith("ej2", StringComparison.OrdinalIgnoreCase) == false)
{
//mY code
}

但是上面的代码过滤了所有的ej2 项目。但我只想过滤具有 author_nameauthor_email 的项目是 pipelinepipeline@gmail.com分别。

如果我改变条件projectName.StartsWith("ej2", StringComparison.OrdinalIgnoreCase) == **true**意思是,它只给出只启动 ej2 的项目。

我该怎么做?

最佳答案

Omit the data which have author_name = pipeline and author_email = pipeline@gmail.com only in the project starts with Ej2 or ej2

试试这个:

if(!author_name.Equals("pipeline",StringComparison.InvariantCultureIgnoreCase) && !author_email.Equals("pipeline@gmail.com ",StringComparison.InvariantCultureIgnoreCase))
{
if(projectName.StartsWith("Ej2", StringComparison.InvariantCultureIgnoreCase))
{
// do Your Operations
}
}

关于c# - 如何在 C# (CA1307) 中使用 StringComparison?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43797166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com