473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2974
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brett | last post by:
I've been trying different methods for replacing escaped strings. None work. I even have a custom Replace() method but still can't get what I need. Some of the characters I need to replace are:...
3
by: Anders Both | last post by:
If I receive a query string on the server that contains escaped char´s like this: text=%20asadfdsf%20%C6%D8%C5%20%E6%F8%E5%20AAA How can I then get the NameValueCollection where the escaped...
2
by: Vance Kessler | last post by:
We are trying write a new ASP.NET page to work with an existing stateless ASP application. The ASP application creates a cookie and of course stores the cookie values as escaped strings (using the...
7
by: Jonny | last post by:
Hi, I am trying to write a C function which will dequote the string in a char * variable, and unescape any escaped quotes, so that, for example: "hello" would become: hello
10
by: Chason Hayes | last post by:
I am trying to convert raw binary data to data with escaped octets in order to store it in a bytea field on postgresql server. I could do this easily in c/c++ but I need to do it in python. I am...
4
by: Trev | last post by:
Hi everyone, Thanks to all who have helped with various issues in the past. I've come up with a new one though: I've run some html through a javascript converter; basically it takes the html and...
12
by: Torsten Bronger | last post by:
Hallöchen! I need some help with finding matches in a string that has some characters which are marked as escaped (in a separate list of indices). Escaped means that they must not be part of...
9
by: Michael Goerz | last post by:
Hi, I am writing unicode stings into a special text file that requires to have non-ascii characters as as octal-escaped UTF-8 codes. For example, the letter "Ã" (latin capital I with acute,...
0
by: stefcollect | last post by:
Hi all, I am pretty new to PHP and am stuck on - what I think - is a generic string handling problem. I need to read and manipulate some HTML files and have a problem in getting some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.