英文:
replace "abc".equals(variable) by variable.equals("abc") in c# code
问题
我维护一个C#项目,发现很多 variable.Equals("abc")
不符合最佳实践。如何一次性替换所有这些比较为这种形式:"abc".Equals(variable)
。我使用Visual Studio。我可以使用正则表达式...
谢谢!
英文:
I maintain a c# project and i found many variable.Equals("abc")
which are not compliant with best practice.
How to replace at once all these comparisons by these form : "abc".Equals(variable)
.
I use visual studio. I'm open with regex...
Thx a lot !
答案1
得分: -1
""abc".equals(variable)
对于遵循最佳实践没有影响"
你应该使用 string.Equals(stringA, stringB, StringComparison.ChooseEnumHere)
ente always the ideal way to compare strings as the StringComparison
enum allows you for finetuned control over case and culture sensitivity of the strings. Use of ==
or strA.Equals(strB)
will always amount to an ordinal (case-sensitive and culture-insensitive) comparison between the strings. In the name of maintainability it is best to explicitly state what you comparison rules are
我强烈建议阅读这部分 documentation 以了解如何正确比较字符串
To directly answer your question, if you want to replace every instance of something with something else, you can use CTRL+SHIFT+H
, which will allow you to search and replace in a much wider scale then just the document or selection (which is what CTRL+H
does). Just be wary that you don't kill some text that you didn't intend to replace
英文:
"abc".equals(variable)
makes no difference to adherance to best practices
What you should be using is string.Equals(stringA, stringB, StringComparison.ChooseEnumHere)
This is always the ideal way to compare strings as the StringComparison
enum allows you for finetuned control over case and culture sensitivity of the strings. Use of ==
or strA.Equals(strB)
will always amount to an ordinal (case-sensitive and culture-insensitive) comparison between the strings. In the name of maintainability it is best to explicitly state what you comparison rules are
I highly recommend reading this bit of documentation to learn about how to go about properly comparing strings
To directly answer your question, if you want to replace every instance of something with something else, you can use CTRL+SHIFT+H
, which will allow you to search and replace in a much wider scale then just the document or selection (which is what CTRL+H
does). Just be wary that you don't kill some text that you didn't intend to replace
答案2
得分: -1
使用正则表达式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication52
{
class Program
{
static void Main(string[] args)
{
string input = @"variable.equals(""abc"") variable.equals(""def"") variable.equals(""ghi"")";
string pattern = @"(^|\s)(?'key1'[^.]+).equals\((?'key2'[^\)]+)\)";
Regex r = new Regex(pattern);
MatchCollection matches = Regex.Matches(input, pattern);
Program c = new Program();
MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceVariable);
string results = r.Replace(input, myEvaluator);
}
public string ReplaceVariable(Match m)
{
return m.Groups["key2"].Value + ".equals(" + m.Groups["key1"].Value + ")";
}
}
}
请注意,我只翻译了您提供的代码部分,没有包含其他内容。
英文:
Using Regex :
<!-- begin snippet: js hide: false console: true babel: false -->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication52
{
class Program
{
static void Main(string[] args)
{
string input = @"variable.equals(""abc"") variable.equals(""def"") variable.equals(""ghi"")";
string pattern = @"(^|\s)(?'key1'[^.]+).equals\((?'key2'[^\)]+)\)";
Regex r = new Regex(pattern);
MatchCollection matches = Regex.Matches(input, pattern);
Program c = new Program();
MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceVariable);
string results = r.Replace(input, myEvaluator);
}
public string ReplaceVariable(Match m)
{
return m.Groups["key2"].Value + ".equals(" + m.Groups["key1"].Value + ")";
}
}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论