Literal string

C

cameljs18

Converting a string variable into a string literal. How do I add the
@ character in front of the string?
I cannot add it when the string is created as it will affect other
parts of the program.

I have tried these but they do not work:

label1.text = @S
label1.text = "@" + S

I want to be able to display the whole string with control characters
in a label.
 
K

Kevin Spencer

First, the only difference between a string variable and a string literal is
that a string variable is a variable that may contain any string, while a
string literal is simply an immutable string. If you think of the variable
as a "box" that can contain any string, which can be an empty box, or have
any string put into it or removed from it, it should be helpful. Perhaps
your confusion arises because the programming syntax allows you to treat a
variable as if it *were* what it contains or points to. It is not.

The '@' token is not a character, but a programming token, which tells the
compiler to treat single backslash characters as literal backslash
characters when compiling a literal string. It is not part of a string, and
is not necessary to use to declare a literal string. So, what you're
actually saying is that you want to display an '@' character arbitrareily in
front of the string for some reason. Perhaps an illustration would help:

"abcde 12345"
@"abcde 12345"

These 2 expressions are exactly the same string. The syntax is simply a way
of providing instructions to the compiler as to what sort of data it should
store, how to identify it, and what the actual data should be.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
J

Jon Skeet [C# MVP]

Converting a string variable into a string literal.

There's no such concept, really.
How do I add the @ character in front of the string?

That's just a compile-time piece of behaviour. It's not captured in
the string itself at all.

In other words, doing:

string s = "\\n";
compiles to *exactly* the same code as
string s = @"\n";
I cannot add it when the string is created as it will affect other
parts of the program.

I have tried these but they do not work:

label1.text = @S
label1.text = "@" + S

I want to be able to display the whole string with control characters
in a label.

You'll need to replace the data in the string itself, e.g.

string tmp = S.Replace("\n", "\\n");
tmp = tmp.Replace("\r", "\\r");

etc.

Jon
 
C

cameljs18

You'll need to replace the data in the string itself, e.g.
string tmp = S.Replace("\n", "\\n");
tmp = tmp.Replace("\r", "\\r");

etc.

Jon

I would like to use replace but the contents of the string is not
always the same. How do I use it then?

Thanks
 
F

Franck

I would like to use replace but the contents of the string is not
always the same. How do I use it then?

Thanks

replace doesn't crash if it has nothing to replace. Replace work as a
kind of filter, you sure itll return the base string but with some
modification if there is to be done. You can replace any character on
an empty string if you want, it will just not found anything to
replace and return you empty string as it was.
 

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