7

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)

0

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

That's a sneaky way of doing it. But if I use C# 6, why would I even want string.Format anymore?
1

No, there is no such functionality.

Comments

1

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.

6 Comments

That doesn't change the corresponding placeholder(s) in the string.
Nope, that changes the order of parameters. Isn't that what OP wants?
+1 for being a cool trick anyway, don't know how I didn't know about that
I wanted to change the order of the parameters and have the place holders keep in sync.
string.Format("{0}, {1}, {2}", foo, bar, fubar). Put the cursor on fubar and hit Ctrl + Alt + Shift + Left. The result is: string.Format("{0}, {1}, {2}", foo, fubar, bar). But the OP wants this: string.Format("{0}, {2}, {1}", foo, fubar, bar)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.