6

Our code base has a lot of StringBuilder.AppendFormats that have strings that end with newline characters. I've made an extension method called AppendLineFormat and would like to use Resharper's Custom Patterns feature to identify and fix those old calls to use the new extension method (and to suggest this new extension method to others in the future).

StringBuilder sb = new StringBuilder();
int i = 1;
sb.AppendFormat("i is {0}\r\n", i);

into

sb.AppendLineFormat("i is {0}", i);

Is there a way to use regex (with replacement) to identify strings that match? I don't want to turn every AppendFormat into an AppendLineFormat - only the ones with strings that end in \r\n. Here's what I've got so far.

Search Pattern:

$sb$.AppendFormat($newLineString$,$args$)

Replace Pattern:

$sb$.AppendLineFormat($newLineString$,$args$)

Where

  • $sb$ is an expression of type System.Text.StringBuilder
  • $newLineString$ is an identifier matching "(.*)\\r\\n" (totally wrong, I know)
  • $args$ is any number of arguments

1 Answer 1

3

Unfortunately as for now it's not supported.

There is a bunch of issues, you can vote for them:

As a workaround you can use Visual Studio Find and Replace feature.

Search pattern:

\.AppendFormat\(\"(.+?)(?>\\r\\n\")

Replace pattern:

.AppendLineFormat("$1"

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.