Automatic refactoring in string.Format

I have a C # project in VS2008 that has many lines of code that look like this:

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

and I would like to cover these lines with one line using string.Format (...).

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

Is there any way to do this automatically?

Edit: Since Mark corrected me, I really don't need to do this, 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 would like to reorganize it into one line of const:

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

(this time itโ€™s more efficient, right?)

resharper can do this automatically in a string, but again, I would like to do this many times.

it would be great to keep the indentation of the lines:

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

but only one long line is also ok.

thank you Leo.

+4
source share
2 answers

From the comments:

it is a much more efficient memory.

No, it is not. This is about the same, with string.Format minimally less efficient since the data is passed in an array (usually GEN0), and you need to parse the format string (which is actually very fast to be fair).

The specification states that the sequence of composite strings is + , i.e.

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

Compiles as:

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

Inside, it is very efficient and does not fulfill the classic problem of โ€œtelescopic stringsโ€. In addition, there are string.Format overloads with different operand numbers to avoid the overhead of a params array. And additional overloads when all operands are strings (which could be even simpler).

In short, it costs you nothing. If you do not, you can provide formats from the outside (quite often in the i18n script), I would leave it alone. All you do is risk the mistakes; you do not make it more effective.

+5
source

Have you tried running Resharper code cleanup? You can create a separate profile that will only make the necessary changes.

0
source

Source: https://habr.com/ru/post/1347140/


All Articles