473,395 Members | 1,905 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,395 software developers and data experts.

Literal string

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.

Oct 19 '07 #1
5 3248
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

<ca*******@yahoo.comwrote in message
news:11**********************@t8g2000prg.googlegro ups.com...
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.

Oct 19 '07 #2
On Oct 19, 1:07 pm, camelj...@yahoo.com wrote:
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

Oct 19 '07 #3
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
Oct 19 '07 #4
On Oct 19, 9:43 am, camelj...@yahoo.com wrote:
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
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.

Oct 19 '07 #5
Another elegant comment on String Theory.
--:-)
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Kevin Spencer" wrote:
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

<ca*******@yahoo.comwrote in message
news:11**********************@t8g2000prg.googlegro ups.com...
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.


Oct 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
8
by: xmllmx | last post by:
It seems true, but I can't find any exact statement on this in the C or C++ standard. The C and C++ standard states: "__FILE_ The presumed name of the source file (a character string...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
1
by: Hetal | last post by:
Hi, I have been working on creating a dynamic table with controls on a ASP.NET webpage and i have been using literal controls to do that. The issue that i am facing is, when i have the Start and...
4
by: zaimoni | last post by:
I've already calculated that the following are valid and should not error, as both just end up with the character literal 'A' being their control expression. The unspecified value of the char*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.