Jonny
Jonny

Reputation: 2519

ReSharper reorder String.Format() arguments

I often find myself writing something daft like:

String.Format("{1}: {0}", reason, message);

or something similar but with far more string placeholders.

Is there an automated refactoring in ReSharper to change the order of the placeholders and the arguments? I have a tendency to mess up the mapping if I try to change the order by hand.

Obviously, the example above is trivial. What I am doing in reality is often writing something like:

String.Format("{0}.{2} = {1}.{3} AND {0}.{4} = {1}.{5} AND {6}.{7} = {1}.{8}",
  table1Alias, table2Alias, col1A, col2A, col1B, col2B, table3Alias, col3C, col2C);

and thinking to myself that it would be great if I could move table3Alias up to the front next to the other aliases.

(ReSharper 7.1.3)

Upvotes: 7

Views: 1368

Answers (3)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

Just place your cursor in table3Alias, then press Ctrl + Alt + Shift + Left/Right arrow. This changes the parameter order in a function call.

There's also an option for removing one when you press Ctrl + Shift + R

enter image description here

There's also a shortcut to add a format item. You may be able to do what you want with combining these. The exact functionality you ask is not implemented.

Upvotes: 1

Chris Xue
Chris Xue

Reputation: 2537

For C# 6+ (ReSharper 10+ / 2016+):

  • Place your cursor at string.Format
  • Press Alt + Enter
  • Select Use string interpolation
  • Press Alt + Enter again
  • Select Convert to string.Format

Upvotes: 4

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

No, there is no such functionality.

Upvotes: 1

Related Questions