c# - 如果条件匹配特定条件,则分配 - c#
问题描述
关于以下逻辑的一个非常基本的问题。
如果该术语以双引号开头,我不想做任何事情。只想将它传递到下一行。但是该行term = term
会引发错误。
不知道如何处理。
if (term.StartsWith("$"))
{
// Don't modify the search term
term = term.Substring(1, term.Length - 1);
originalTerm = string.Copy(term);
}
else
{
if (term.StartsWith("\""))
{
term = term;
}
else
{
// Escape special characters
term = LuceneReservedCharacterPattern.Replace(term, @"\$0");
// Then modify the term to improve phrase match:
// lyme disease OR "lyme disease"^2
term = string.Concat("(", term, ") OR \"", term, "\"^2");
}
if (collections != null && !collections.Any(c => c.Equals("all", StringComparison.OrdinalIgnoreCase)))
{
// check for multiple collections collections: (all OR this OR that OR him OR her) etc
term = string.Concat("(", term, ") AND collections:(", string.Join(" OR ", collections), ")");
}
}
解决方案
只需反转条件并删除else
..?这样,如果条件是true
,则什么都不会发生。
推荐阅读
- django - 如何在 django rest 框架和 docker 中设置 prometheus
- java - “规则”与同名变量的差异
- c# - .net core 3.1 本地化 resx 命名的明显重大变化?
- python - Django {% for loop - 显示 3 个值中的 1 个
- cmake - Clang:使用 Cmake 为我的项目构建 compile_commands.json
- node.js - 使用 Docker Compose 将单独的 Postgres 和 Node 容器连接在一起
- python-3.x - 多类语义分割模型评估
- bash - 我正在使用 Pandoc 将 Markdown 转换为 .docx - 如何从生成的文件名中删除 .md?
- php - PHP通过引用传递类中相同对象的函数
- jestjs - Jest 过时快照警告导致测试失败:退出 Jest 时忽略警告