Escaped string in C#

  • Thread starter Thread starter Oxns
  • Start date Start date
O

Oxns

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
 
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.
 
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.
 
Oxns said:
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);
 
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 said:
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.
 
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.
 
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.
 
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.
 
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,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top