By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Simple / stupid string question

P: n/a
I have a richtextbox which I want the "literal" rtf of.
richtextbox.rtf returns
{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc 1 }\r\n\0

when i put this into a string I get
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\u c1 }\r\n\0"
I want this to be
@"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\ uc1 }\r\n\0"
to treat the escape characters as literals

How can I do this?

TIA
JB
*Rubs eyes and stares blankly at screen*
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"John Baro" <jo***@NixSpaMiprimus.com.au.com.au> wrote in message
news:2h************@uni-berlin.de...
I have a richtextbox which I want the "literal" rtf of.
richtextbox.rtf returns
{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc 1 }\r\n\0

when i put this into a string I get
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\u c1 }\r\n\0"
What do you mean by "put this into a string"?
I want this to be
@"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\ uc1 }\r\n\0"
to treat the escape characters as literals

How can I do this?


Can you explain a little more about how to demonstrate the problem and why
you think something is going wrong?

Nov 16 '05 #2

P: n/a
Hi Michael

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address> wrote
in message news:ub**************@tk2msftngp13.phx.gbl...

"John Baro" <jo***@NixSpaMiprimus.com.au.com.au> wrote in message
news:2h************@uni-berlin.de...
I have a richtextbox which I want the "literal" rtf of.
richtextbox.rtf returns
{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc 1 }\r\n\0

when i put this into a string I get
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\u c1 }\r\n\0"


What do you mean by "put this into a string"?


string s = MyRtb.RTF;
I want this to be
@"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\ uc1 }\r\n\0"
to treat the escape characters as literals

How can I do this?


Can you explain a little more about how to demonstrate the problem and why
you think something is going wrong?


I want to perform a regex match on the rtf but without the @ at the
beginning of the string, indicating its a literal, I think the regex match
is treating the \\ as a single \. ( I can match on a single \ instead of the
escaped double but I would prefer to match on the literal string)

When I put \n for instance into the rtb then the text property is @"\n",
indicating that \n is a literal, not a newline command.

Take this text

@"\n"
is the same as
"\\n"
/*

@"\\n"
is the same as
\\\\n

I hope this is clearer.
Cheers
JB
Nov 16 '05 #3

P: n/a
Slightly clearer... but I haven't worked with regexes myself.

What I know is that "abc" and @"abc" are not two kinds of strings. They are
two different things you can do to put the same sequence of characters (a,
b, c) into memory. Likewise, "\\" and @"\" are not two different strings;
they are two ways of writing the same string.

So asking how to put the @ at the beginning is not the right question. When
it's in the computer's memory, the string neither has nor lacks the initial
@ or the quotation marks. It is just a series of characters.

It sounds like your question has to do with how regexes handle backslashes,
and, alas, I don't know the answer.
"John Baro" <jo***@NixSpaMiprimus.com.au.com.au> wrote in message
news:2h************@uni-berlin.de...
Hi Michael

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address> wrote in message news:ub**************@tk2msftngp13.phx.gbl...

"John Baro" <jo***@NixSpaMiprimus.com.au.com.au> wrote in message
news:2h************@uni-berlin.de...
I have a richtextbox which I want the "literal" rtf of.
richtextbox.rtf returns
{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc 1 }\r\n\0

when i put this into a string I get
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\u c1 }\r\n\0"
What do you mean by "put this into a string"?


string s = MyRtb.RTF;
I want this to be
@"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\ uc1 }\r\n\0"
to treat the escape characters as literals

How can I do this?


Can you explain a little more about how to demonstrate the problem and why you think something is going wrong?


I want to perform a regex match on the rtf but without the @ at the
beginning of the string, indicating its a literal, I think the regex

match is treating the \\ as a single \. ( I can match on a single \ instead of the escaped double but I would prefer to match on the literal string)

When I put \n for instance into the rtb then the text property is @"\n",
indicating that \n is a literal, not a newline command.

Take this text

@"\n"
is the same as
"\\n"
/*

@"\\n"
is the same as
\\\\n

I hope this is clearer.
Cheers
JB

Nov 16 '05 #4

P: n/a

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address> wrote
in message news:es*************@TK2MSFTNGP11.phx.gbl...
Slightly clearer... but I haven't worked with regexes myself.

What I know is that "abc" and @"abc" are not two kinds of strings. They are two different things you can do to put the same sequence of characters (a,
b, c) into memory. Likewise, "\\" and @"\" are not two different strings;
they are two ways of writing the same string.
Yes :)
So asking how to put the @ at the beginning is not the right question. When it's in the computer's memory, the string neither has nor lacks the initial @ or the quotation marks. It is just a series of characters.
However. Once we have a string that is not a "literal" string. How can we
convert it to a "literal" string.

ie

string s = @"\\n\t";
string x = "\\n\t";
string p = s;
string t = x;

How can we make x = equal @"\\n\t" without setting x to s;

This might work
t = x.Replace(@"\", @"\\"); //Nope, only replaces the first "\" because it
treats the \t as a tab char

It sounds like your question has to do with how regexes handle backslashes, and, alas, I don't know the answer.
I was actually wrong to be testing for double forward slashes. The string
has already been escaped so I should test for single ones :) (which works)

I am still curious to know how to convert an escaped string into a literal
string so that by the above example we have t equal to "\\\\n\\t" or
@"\\n\t"
(Cant think why you would want to do this but am still curious :)

Cheers
JB

"John Baro" <jo***@NixSpaMiprimus.com.au.com.au> wrote in message
news:2h************@uni-berlin.de...
Hi Michael

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address>

wrote
in message news:ub**************@tk2msftngp13.phx.gbl...

"John Baro" <jo***@NixSpaMiprimus.com.au.com.au> wrote in message
news:2h************@uni-berlin.de...
> I have a richtextbox which I want the "literal" rtf of.
> richtextbox.rtf returns
> {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc 1 }\r\n\0
>
> when i put this into a string I get
> "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\u c1 }\r\n\0"

What do you mean by "put this into a string"?


string s = MyRtb.RTF;
> I want this to be
> @"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\ uc1 }\r\n\0"
> to treat the escape characters as literals
>
> How can I do this?

Can you explain a little more about how to demonstrate the problem and why you think something is going wrong?


I want to perform a regex match on the rtf but without the @ at the
beginning of the string, indicating its a literal, I think the regex

match
is treating the \\ as a single \. ( I can match on a single \ instead of

the
escaped double but I would prefer to match on the literal string)

When I put \n for instance into the rtb then the text property is @"\n",
indicating that \n is a literal, not a newline command.

Take this text

@"\n"
is the same as
"\\n"
/*

@"\\n"
is the same as
\\\\n

I hope this is clearer.
Cheers
JB


Nov 16 '05 #5

P: n/a
> However. Once we have a string that is not a "literal" string. How can we
convert it to a "literal" string.

ie

string s = @"\\n\t"; <--- that is: s contains backslash backslash n backslash t string x = "\\n\t"; <--- that is: x contains backslash newline tab
string p = s;
string t = x;

How can we make x = equal @"\\n\t" without setting x to s;
Ah! You're wanting to insert all the escape characters into a string that
needs them.
I am still curious to know how to convert an escaped string into a literal
string so that by the above example we have t equal to "\\\\n\\t" or
@"\\n\t"
(Cant think why you would want to do this but am still curious :)

This might work
t = x.Replace(@"\", @"\\"); //Nope, only replaces the first "\" because it
treats the \t as a tab char


Or rather because the elements of x *are* already backslash, newline, tab.
It's not a matter of how they get "treated"... That's the reason for my
harping on the distinction between the contents of a string and the notation
with which it is written.

What you want is to go through the string and replace:
backslash with double backslash
newline with backslash n
tab with backslash t
return with backslash r
and a number of others. These are all separate replacements. There is no
shortcut, because there is no physical attribute of a tab or a newline that
makes it map onto \t or \n respectively; those are arbitrary codes.

This of course makes it a different string which a regex routine might use
to recognize newlines and tabs, even though it doesn't have any newlines and
tabs in it.

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.