471,605 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Escaped string in C#

Hi,

Can anyone point me at the class to convert a string so that it displays
escaped chars as \r, \n etc. please

Its done in the 2005 debugger so I hope its available as a class ??.

Thanks

Graham
Dec 22 '05 #1
13 2822
Graham,

Put an @ in front of the string, example:

string myString = @"\r\n";

Steve

Dec 22 '05 #2
Come to my forum to get help: http://www.wizardsolutionsusa.com

Dec 22 '05 #3
Steve,

Thanks for the vary quick reply :-).

This is the wrong way round however - I already have my string with embedded
control characters - and simply want to display this - with control chars
'shown as escaped (eg \r\n etc.).

Thanks

G.

"steve813" <st******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Graham,

Put an @ in front of the string, example:

string myString = @"\r\n";

Steve

Dec 22 '05 #4
cs*********@gmail.com wrote:
Come to my forum to get help: http://ww


Why would anyone do that when this forum has many experts that really
know their stuff and offer their help willingly day in and day out. Go
fishing elsewhere.
--
Tom Porterfield
Dec 22 '05 #5
Oxns wrote:
Steve,

Thanks for the vary quick reply :-).

This is the wrong way round however - I already have my string with embedded
control characters - and simply want to display this - with control chars
'shown as escaped (eg \r\n etc.).


string s1 = "\r\n";
string s2 = System.Text.RegularExpressions.Regex.Escape(s1);
--
Tom Porterfield
Dec 22 '05 #6
Are you simply saying that you want to convert the control characters to
"text" that represents the characters.

If so, you might do something like myString.Replace("\r\n", @"\r\n");

"Oxns" <ox**@community.nospam> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
Steve,

Thanks for the vary quick reply :-).

This is the wrong way round however - I already have my string with
embedded control characters - and simply want to display this - with
control chars 'shown as escaped (eg \r\n etc.).

Thanks

G.

"steve813" <st******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Graham,

Put an @ in front of the string, example:

string myString = @"\r\n";

Steve


Dec 22 '05 #7
Oxns <ox**@community.nospam> wrote:
Can anyone point me at the class to convert a string so that it displays
escaped chars as \r, \n etc. please

Its done in the 2005 debugger so I hope its available as a class ??.


Not that I know of, but it's very easy to do. Just run through a series
of replacements;

string replaced = original.Replace ("\\", "\\\\")
.Replace ("\r", "\\r")
.Replace ("\n", "\\n")
.Replace ("\'", "\\\'")
.Replace ("\"", "\\\")

(etc)

See http://www.pobox.com/~skeet/csharp/faq/#escapes for the complete
list.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 22 '05 #8
Effectively yes - but for all escaped characters. I can do this the hard way
but assumed/hoped that there was a framework method somewhere which would do
this for me :-O.

Thanks

G.

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uf**************@TK2MSFTNGP14.phx.gbl...
Are you simply saying that you want to convert the control characters to
"text" that represents the characters.

If so, you might do something like myString.Replace("\r\n", @"\r\n");

"Oxns" <ox**@community.nospam> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
Steve,

Thanks for the vary quick reply :-).

This is the wrong way round however - I already have my string with
embedded control characters - and simply want to display this - with
control chars 'shown as escaped (eg \r\n etc.).

Thanks

G.

"steve813" <st******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Graham,

Put an @ in front of the string, example:

string myString = @"\r\n";

Steve



Dec 22 '05 #9
Tom,

Yes - I had a look at this but doesn't it only translate regex chars - and
not all string escaped chars ??.

Looks like I'll just do it the hard way - as ever ;-)).

G.

"Tom Porterfield" <tp******@mvps.org> wrote in message
news:eC**************@TK2MSFTNGP14.phx.gbl...
Oxns wrote:
Steve,

Thanks for the vary quick reply :-).

This is the wrong way round however - I already have my string with
embedded control characters - and simply want to display this - with
control chars 'shown as escaped (eg \r\n etc.).


string s1 = "\r\n";
string s2 = System.Text.RegularExpressions.Regex.Escape(s1);
--
Tom Porterfield

Dec 22 '05 #10
Tom,

My thoughts exactly - hence why I didn't reply ;-)).

G.

"Tom Porterfield" <tp******@mvps.org> wrote in message
news:en**************@TK2MSFTNGP15.phx.gbl...
cs*********@gmail.com wrote:
Come to my forum to get help: http://ww


Why would anyone do that when this forum has many experts that really know
their stuff and offer their help willingly day in and day out. Go fishing
elsewhere.
--
Tom Porterfield

Dec 22 '05 #11
Jon,

Yeah - I know, just get fed up doing things the hard way - then finding
thats its a problem already solved in the framework ;-)).

Thanks

G.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Oxns <ox**@community.nospam> wrote:
Can anyone point me at the class to convert a string so that it displays
escaped chars as \r, \n etc. please

Its done in the 2005 debugger so I hope its available as a class ??.


Not that I know of, but it's very easy to do. Just run through a series
of replacements;

string replaced = original.Replace ("\\", "\\\\")
.Replace ("\r", "\\r")
.Replace ("\n", "\\n")
.Replace ("\'", "\\\'")
.Replace ("\"", "\\\")

(etc)

See http://www.pobox.com/~skeet/csharp/faq/#escapes for the complete
list.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 22 '05 #12
>Can anyone point me at the class to convert a string so that it displays
escaped chars as \r, \n etc. please

Its done in the 2005 debugger so I hope its available as a class ??.


The C# CodeDOM provider does it for you. Try this

static string GetCSharpStringLiteral(string value)
{
ICodeGenerator cg = new CSharpCodeProvider().CreateGenerator();
using (StringWriter sw = new StringWriter())
{
cg.GenerateCodeFromExpression(new CodePrimitiveExpression(value),
sw, new CodeGeneratorOptions());
return sw.ToString();
}
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 22 '05 #13
Mattias,

Thanks for this but V2 Framework tells me this method is obsolete.

Think I'll stick to my own solution of hand-crafted string replacements ;-))
Seems to be simpler than caling CSharpe compiler functions to do a simple
string manipulation :-O.

regards

graham

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
Can anyone point me at the class to convert a string so that it displays
escaped chars as \r, \n etc. please

Its done in the 2005 debugger so I hope its available as a class ??.


The C# CodeDOM provider does it for you. Try this

static string GetCSharpStringLiteral(string value)
{
ICodeGenerator cg = new CSharpCodeProvider().CreateGenerator();
using (StringWriter sw = new StringWriter())
{
cg.GenerateCodeFromExpression(new CodePrimitiveExpression(value),
sw, new CodeGeneratorOptions());
return sw.ToString();
}
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Dec 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Brett | last post: by
3 posts views Thread by Anders Both | last post: by
2 posts views Thread by Vance Kessler | last post: by
7 posts views Thread by Jonny | last post: by
10 posts views Thread by Chason Hayes | last post: by
4 posts views Thread by Trev | last post: by
12 posts views Thread by Torsten Bronger | last post: by
9 posts views Thread by Michael Goerz | last post: by
reply views Thread by stefcollect | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.