replace "abc".equals(variable) by variable.equals("abc") in c# code

huangapple go评论73阅读模式
英文:

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 = @&quot;variable.equals(&quot;&quot;abc&quot;&quot;) variable.equals(&quot;&quot;def&quot;&quot;) variable.equals(&quot;&quot;ghi&quot;&quot;)&quot;;

            string pattern = @&quot;(^|\s)(?&#39;key1&#39;[^.]+).equals\((?&#39;key2&#39;[^\)]+)\)&quot;;
            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[&quot;key2&quot;].Value + &quot;.equals(&quot; + m.Groups[&quot;key1&quot;].Value + &quot;)&quot;; 


        }
    }
 
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 22:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定