views:

100

answers:

6

Is there a way in Visual Studio to convert text to a C# string literal and back?

For example if I have the text:

Lorem ipsum dolor sit amet, consectetuer adipiscing 
elit, sed diam nonummy nibh euismod tincidunt ut 
laoreet dolore magna aliquam erat volutpat.

can I easily convert it to:

"Lorem ipsum dolor sit amet, consectetuer adipiscing\n" + 
"elit, sed diam nonummy nibh euismod tincidunt ut\n" +
"laoreet dolore magna aliquam erat volutpat."

and then convert it back using a macro or some utility?

+4  A: 

You know you can do

@"Lorem ipsum dolor sit amet, consectetuer adipiscing  
elit, sed diam nonummy nibh euismod tincidunt ut  
laoreet dolore magna aliquam erat volutpat."

right? @-strings (a.k.a. "verbatim string literals") can contain newlines.

Brian
However, that won't help for quotes.
SLaks
You're correct on that; those would have to be escaped with "".
MisterZimbu
Indeed, though highlighting the string and Ctrl-H find-and-replace-in-region of " to "" works pretty well.
Brian
A: 

DevExpress CodeRush can do this.

SLaks
So does ReSharper.
Julien Lebosquain
A: 

Yes. Use:

string s = @"Lorem ipsum dolor sit amet, consectetuer adipiscing  
elit, sed diam nonummy nibh euismod tincidunt ut  
laoreet dolore magna aliquam erat volutpat. 
";
icemanind
Unless the string has quotes.
SLaks
+1  A: 

If you prefix the string with an @ you can get something similar:

string lorem = @"Lorem ipsum dolor sit amet, consectetuer adipiscing 
elit, sed diam nonummy nibh euismod tincidunt ut 
laoreet dolore magna aliquam erat volutpat.";

This will preserve the whitespace.

MisterZimbu
A: 

If you don't mind extra whitespace, you can enclose it in quotes and include the @ symbol at the beginning which means 'literal.' Like this:

string s = @"Lorem ipsum dolor sit amet, consectetuer adipiscing 
elit, sed diam nonummy nibh euismod tincidunt ut 
laoreet dolore magna aliquam erat volutpat.";
JYelton
+2  A: 

there are add ons for that take a look here http://arcanecode.com/2006/11/20/visual-studio-add-ins-paste-as/

Itay
Great. Smart paster does most of what I need. I can live without going the other direction for now.
FigBug
I am glad I could Help :)
Itay