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.

This entry was posted in 10411, 2666, 2672, 2673, 2675, 2676, 2677, 3302. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>