|
-
February 16th, 2005, 04:09 AM
#1
How to print "1" ?
Hi,
I have an application in C#. i want to display a value of a variable using quotes.
e.g.
int i = 1;
string str = " u have chosen" + i+;
label1.text = str; // to display the value of str
now i want to print this value in quotes like "1"
i want my output to be like this:
u have chosen "1" .
Can anybody help me?????
-
February 16th, 2005, 04:18 AM
#2
Re: How to print "1" ?
string str = " u have chosen \"" + i.ToString()+"\"";
-
February 16th, 2005, 04:19 AM
#3
Re: How to print "1" ?
Code:
int i = 1;
label1.Text = string.Format("u have chosen \"{0}\"", i + 1);
Darwen.
-
February 16th, 2005, 04:27 AM
#4
Re: How to print "1" ?
Prefix the string with '@' and put double quatation ("") within the string to make it part of the string. Following example puts "1" into string. Please modify the code as per your needs.
Code:
string str=@"<You have chosen=""1"">";
Regards.
-
February 16th, 2005, 04:39 AM
#5
Re: How to print "1" ?
Hey - let's see how many ways we can do this ? Hehe...
Darwen.
-
February 16th, 2005, 04:50 AM
#6
Re: How to print "1" ?
Hi,
Ajay Vijay
solution which u have given is not working. Anyway, thanks. Now i got the solutions.
-
February 16th, 2005, 04:53 AM
#7
Re: How to print "1" ?
 Originally Posted by khushi
solution which u have given is not working. Anyway, thanks. Now i got the solutions.
Can you post the code you used to initialize the string? The '@' operator is provided in C# langauge for easing the escaping special characters.
-
February 16th, 2005, 05:59 AM
#8
Re: How to print "1" ?
Hey - let's see how many ways we can do this ? Hehe...
Last One who provide valid solutions WINS!
string str = " u have chosen \x22" + i.ToString()+"\x22";
Krzemo.
-
February 16th, 2005, 06:48 AM
#9
Re: How to print "1" ?
There's a lot of temporary strings being created there I would say Krzemo :
I prefer my solution of string.Format.
I could produce a solution using System.Text.Encoding.ASCII.
Or even Marshal.PtrToStringAnsi or the like.
Darwen.
Last edited by darwen; February 16th, 2005 at 06:51 AM.
-
February 16th, 2005, 06:56 AM
#10
Re: How to print "1" ?
Okay, what about:
Code:
"D:\WINDOWS\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}\TempDir"
Which one would you suggest.
See '@' operator is provided for program code' clarity and to avoid potential problems.
In Console apps you must have seen animation that resembles "hourglass" concept using "-\|/" string, but strings like this are error prone. So, C#'s operator '@' should be used.
-
February 16th, 2005, 07:01 AM
#11
-
February 16th, 2005, 08:05 AM
#12
Re: How to print "1" ?
I only put that because he's got 'i+' at the end of his original post : I presumed he forgot the +1 or something.
OK how about this for a convoluted way of doing it :
Code:
int i = 1;
byte [] abText = System.Text.Encoding.ASCII.GetBytes(sInitial);
byte [] abNumber = System.Text.Encoding.ASCII.GetBytes(i.ToString());
byte [] abAll = new byte[abText.Length + abNumber.Length];
Buffer.BlockCopy(abText, 0, abAll, 0, abText.Length);
Buffer.BlockCopy(abNumber, 0, abAll, abText.Length, abNumber.Length);
label.Text = System.Text.Encoding.ASCII.GetString(abAll);
Darwen.
-
February 16th, 2005, 08:06 AM
#13
Re: How to print "1" ?
StringBuilder method :
Code:
int i = 1;
StringBuilder builder = new StringBuilder();
builder.Append("u have chosen ");
builder.Append(i.ToString());
label1.Text = builder.ToString();
Darwen.
-
February 16th, 2005, 04:57 PM
#14
Re: How to print "1" ?
I have a question too. Which one of the posted solution is taking the smalles amount of time? In C++ the string format functions are usually time consuming, and if you need to do a lot of formats in a loop then problems may arise. What's the case in C#?
-
February 16th, 2005, 07:39 PM
#15
Re: How to print "1" ?
Best case is probably the StringBuilder - especially if you estimate the original size e.g.
Code:
int i = 1; // or whatever value
string sMessage = "u have chosen \"";
int nEstimateOfIntSize = (i <= 0) ? 1 : 0;
int iCount = i;
while (iCount != 0)
{
nEstimateOfIntSize += 1;
iCount /= 10;
}
StringBuilder builder =
new StringBuilder(sMessage.Length + nEstimateOfIntSize + 1);
builder.Append(sMessage);
builder.Append(i.ToString());
builder.Append("\"");
string sResult = builder.ToString();
The StringBuilder will allocate the memory after every Append, but if using its default constructor will set its start buffer size at zero. By setting its 'capacity' i.e. the amount of memory it initially allocates it's far more efficient, because it doesn't need to re-allocate (i.e. create & copy) the memory to do any particular Append.
The above case is the most efficient : i.e. when it only allocates enough memory for a particular resultant string.
Darwen.
Last edited by darwen; February 16th, 2005 at 07:54 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|