Batch-convert c # code to use String.Format

Possible duplicate:
Shortcut string.format

In our large code base, unfortunately, there are many such statements:

MessageBox.Show("Hello, " + userName + "!"); 

Obviously, it would be better to facilitate the externalization of strings:

 MessageBox.Show(String.Format("Hello, {0}!", userName)); 

Is there any tool that can automate this task (convert string concatenation to String.Format)?

If not, I believe it is necessary to find a parser that builds a syntax tree for a given C # source file, find expressions that perform string concatenations in this tree (doesn't seem easy) and convert the found occurrences?

EDIT : DevExpress CodeRush does exactly what I want - pretty cool. But, unfortunately, it only works with the current source file, and I have to click every appearance of it (or, as it seems). I would like to have an operation in all of my source files (for example, with Replace All).

+4
source share
3 answers

Check out DevExpress CodeRush . This makes it easy to find and reorganize these problems. Although I donโ€™t know if you can automate refactoring. ReSharper has a similar feature.

+1
source

ReSharper supports this action by pressing Alt + Enter Enter .

Read more about it here: Abbreviated shortcut string.format

+1
source

If you donโ€™t have the tools, just replace Everything in Visual Studio.

Search:

 "([^"]*)"\s*\+\s*(:i) 

Replaced by:

 String.Format("\1#{0}", \2) 

Unfortunately, I do not have access to Visual Studio to test these expressions. You may need to use :b instead of \s in the find statement.

+1
source

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


All Articles