Simple / stupid string question

J

John Baro

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

when i put this into a string I get
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\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*
 
M

Michael A. Covington

John Baro said:
I have a richtextbox which I want the "literal" rtf of.
richtextbox.rtf returns
{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0

when i put this into a string I get
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\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?
 
J

John Baro

Hi Michael

Michael A. Covington said:
What do you mean by "put this into a string"?

string s = MyRtb.RTF;
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
 
M

Michael A. Covington

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.
 
J

John Baro

Michael A. Covington said:
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
 
M

Michael A. Covington

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.
 

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

Top