自动重构为 string.Format

发布于 2024-10-31 04:58:16 字数 1272 浏览 4 评论 0原文

我在 VS2008 中有一个 ac# 项目,其中有很多代码行,如下所示:

string s = "bla blab" + x + "bla bla bla" + y + .... ;

我想使用 string.Format(...) 将这些字符串转换为单个字符串。

我目前使用的是Resharper 5.0,我可以一键重构一行代码。 问题是我有超过 1000 行这样的行,而且我不想手动检查每一行。

有没有办法自动执行此操作?

编辑: 正如马克纠正我的那样,我真的不需要这样做, 但我还有另一个非常相似的问题: 我得到了这段代码

 string s = "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa";

(该字符串是一些 sql 查询)

,我想将其重构为一个 const 字符串:(

string s = "aaaaaaaaaaaaaaaaaa....aaaaa";

这一次,它更高效,对吧?)

resharper 可以针对每个字符串自动执行此操作, 但话又说回来,我愿意做很多次。

保持行缩进会很棒:

string s = @"aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa";

但只有一行长行也可以。

谢谢, 列弗。

I have a c# project in VS2008 that have a lot of lines of code that look like this:

string s = "bla blab" + x + "bla bla bla" + y + .... ;

and I would like to covert those strings to a single string using string.Format(...).

I am currently using Resharper 5.0, and I can refactor a single line of code with one click.
The problem is that I have more that a 1000 lines like this, and I don't want to manually go over each line.

Is there a way to do this automatically?

Edit:
As Marc corrected me, I dont really need to do it,
but I have another very similar problem:
I got this code

 string s = "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa" +
                   "aaaaaaaaaaaaaaaaaaaaaaaa";

(the string is some sql query)

and I'd like to refactor it to one const string:

string s = "aaaaaaaaaaaaaaaaaa....aaaaa";

(this time, it is more efficient, right?)

resharper can do this automatically per string,
but again, I'd like to do it a lot of times.

it would be great to keep the lines indentation:

string s = @"aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa
                    aaaaaaaaaaaaaaaaaaaaaaaa";

but just one long line is ok too.

thanks,
Lev.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇柔作态 2024-11-07 04:58:16

来自评论:

内存效率更高

,不,事实并非如此。它大约相同,string.Format边际不太效率,如数据以数组形式传递(通常是GEN0),并且需要解析格式字符串(公平地说,这实际上非常快)。

在规范中定义复合字符串 + 序列,即

string s = "abc" + x + "def" + y;

编译为:

string s = string.Concat("abc", x, "def", y);

在内部,这是非常有效的,并且不会执行经典的“伸缩字符串”问题。此外,还有 string.Format 的重载,它采用不同数量的操作数,以避免 params-array 开销。当所有操作数都是字符串时,还有额外的重载(这样可以更容易)。

简而言之,这不会花费您任何费用。除非你这样做是为了可以从外部提供格式(在 i18n 场景中很常见),否则我不会管它。你所做的一切都是在冒着 bug 的风险;你并没有让它变得更有效率。

From comments:

it's much more efficient memory wise

No, it isn't. It is about the same, with string.Format marginally less efficient, as the data is passed in an array (usually GEN0), and it is necessary to parse the format string (which is actually very fast, to be fair).

It is defined in the spec that a composite string + sequence, i.e.

string s = "abc" + x + "def" + y;

Is compiled as:

string s = string.Concat("abc", x, "def", y);

Internally, this is very efficient, and doesn't do the classic "telescoping strings" problem. Additionally, there are overloads of string.Format taking various numbers of operands, to avoid even the params-array overhead. And additional overloads for when all operands are strings (which it can so even easier).

In short, this is costing you nothing. Unless you are doing this so you can supply the formats externally (quite common in an i18n scenario), I would leave it alone. All you are doing is risking bugs; you aren't making it more efficient.

风向决定发型 2024-11-07 04:58:16

您是否尝试过运行 Resharper 代码清理?您可以创建一个单独的配置文件,仅进行您想要的更改。

Have you tried running Resharper code cleanup? You can create a separate profile which only makes the changes you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文