VS REGEX: Joining Concatenated Strings

I don’t like to see constant strings being concatenated in source code. I know the compiler compiles it into a single string, but I think it sends out the wrong message.

However, code generators (like Visual Studio’s Unit Test generator) usually generate code with long strings concatenated across several lines of source code.

In order to replace this:

text = "This is a one " + "line concatenation." +
       "This is a multiline concatenation."
       + "This is a multiline concatenation."


       +



       "This is a multiline concatenation with multiple blank lines." +
       "This looks like a string concatenation \" + " + "."

into this:

text = "This is a one line concatenation.This is a multiline concatenation.This is a multiline concatenation.This is a multiline concatenation with multiple blank lines.This looks like a string concatenation \" + ."



The following regular expression can be used:

Find what:
~(\\)\”:b*(:b*\n)*\+(:b*\n)*:b*\”
Replace with:
 

There’s one caveat, though. This Regular expression only works with regular C# string literals. It doesn’t work with Visual Basic or @-quoted C# strings.

Check out the complete list.