String Manipulation - Escape Sequence

G

Guadala Harry

I need to place the following into a string... How can I properly escape the
% " / < and > characters?

<table width="100%" border="0" cellspacing="0" cellpadding="4px"
class="hfAll"></Table>

Thanks.
 
S

Scott Allen

Hi GMan:

The backslash will escape the " character. %, /, <, and > do not need
an escape sequence.

You can also use the @ symbol to start a verbatim string literal.

So:

Console.WriteLine("This \"is\" a %<>test\\");
Console.WriteLine(@"This ""is"" a %<>test\");

both print out This "is" a %<>test\

For your string I'd just escape all the " characters with \.

HTH,
 
M

Matias Woloski

The only thing needed to escape here are the doublequote and you escape it
using the backslash. For instance

string html = "<table width=\"100%\" border .... >"

Matias Woloski
southworks.net
 
G

Guadala Harry

Thanks - I got into trouble when I used both the @ symbol and the \ escape
character... I received the IDE/tooltop message "; expected" That's why I
thought I needed to escape more than just the " character - but could find
no references including the other characters in the "need to escape these"
lists.

oh the joys of being a C# beginner...

: )

-G
 
M

mikeb

Guadala said:
Thanks - I got into trouble when I used both the @ symbol and the \ escape
character...

That's because in a 'verbatim string literal' (one preceded by the @
character), escaping is done differently and can only be done for the
double-quote character:

string s1 = @" "" "; // this is a lone double-quote char
// surrounded by some spaces

string s2 = " \" "; // the same string using standard
// character escaping

string s3 = @" \" "; // invalid syntax - you have a verbatim
// string that is some spaces with a
// backslash at the end. This is
// followed by a spurious double-
// quote char right before the semi-colon.
 

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