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?????
Krzemo
February 16th, 2005, 03:18 AM
string str = " u have chosen \"" + i.ToString()+"\"";
darwen
February 16th, 2005, 03:19 AM
int i = 1;
label1.Text = string.Format("u have chosen \"{0}\"", i + 1);
Darwen.
Ajay Vijay
February 16th, 2005, 03:27 AM
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.string str=@"<You have chosen=""1"">";Regards.
darwen
February 16th, 2005, 03:39 AM
Hey - let's see how many ways we can do this ? Hehe...
Darwen.
khushi
February 16th, 2005, 03:50 AM
Hi,
Ajay Vijay
solution which u have given is not working. Anyway, thanks. Now i got the solutions.
Ajay Vijay
February 16th, 2005, 03:53 AM
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.
Krzemo
February 16th, 2005, 04:59 AM
Hey - let's see how many ways we can do this ? Hehe...
Last One who provide valid solutions WINS! :D :D :D
string str = " u have chosen \x22" + i.ToString()+"\x22";
Krzemo.
darwen
February 16th, 2005, 05:48 AM
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.
Ajay Vijay
February 16th, 2005, 05:56 AM
Okay, what about:"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.
Krzemo
February 16th, 2005, 06:01 AM
There's a lot of temporary strings being created there I would say Krzemo :
I would say: only 1 more (maybe 2 but I suspect that only one) - is it "a lot" ?:rolleyes:
And another thing - your solution cannot be counted in the competition "Last One who provide valid solutions WINS!" because it is not valid :p .
:D
IMHO this "string.Format("u have chosen \"{0}\"", i + 1);" will produce output like that:
u have chosen "2":thumbd:
Best regards,
Krzemo.
:)
darwen
February 16th, 2005, 07:05 AM
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 :
int i = 1;
StringBuilder builder = new StringBuilder();
builder.Append("u have chosen ");
builder.Append(i.ToString());
label1.Text = builder.ToString();
Darwen.
cilu
February 16th, 2005, 03:57 PM
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#?
darwen
February 16th, 2005, 06:39 PM
Best case is probably the StringBuilder - especially if you estimate the original size e.g.
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.
Krzemo
February 16th, 2005, 07:30 PM
Usually string builder is the fastest way to buid string.... but IMHO not in this case. Especialy when darwen uses that loop and conditional assigment ;) ...
but if using its default constructor will set its start buffer size at zeroNo - its starts at 16.
The above case is the most efficient Im not so sure....
disregarding the initial decoration of code :D :D , there are 3 temporary strings, one temporary object and 5 copy instructions.
I'm not saying that string builder is slower, but string builder is designed for fast building of large strings.With smaller strings it can be actually slower than concatenting strings with "+".
darwen: Last solution ... looks like it can produce valid output - so for the moment U are a winner! :D
int nEstimateOfIntSize = 1+(int)System.Math.Log10(i);
Krzemo
February 17th, 2005, 02:46 AM
Too much code here. This is the same.
And I have shorter one:
int nEstimateOfIntSize =10
Why? - because more precision is not needed.StringBuilder operations will not gain any performance boost if initial storage size will be better fited. (in extreeme case it will allocate space for 9 chars too much)... so better use max length of int string (=10).
darwen:
label1.Text = "u have chosen \"\"".Insert(15,i.ToString());
or
label1.Text = "u have chosen \"#\"".Replace("#",i.ToString());
:D
Best regards,
Krzemo.
darwen
February 17th, 2005, 03:40 AM
Wouldn't it be just as easy and efficient to do my original suggestion ?
string sMessage = string.Format("u have chosen \"{0}\"", i);
Yeah I know that loop was horrible but I was in a bit of a rush and not thinking.
Darwen.
cilu
February 17th, 2005, 03:42 AM
Why? - because more precision is not needed.StringBuilder operations will not gain any performance boost if initial storage size will be better fited. (in extreeme case it will allocate space for 9 chars too much)... so better use max length of int string (=10).
Didn't we all forgot something here? The possible '-' sign...
-1,234,567,890
Well, Darwen didn't.
int nEstimateOfIntSize = (i <= 0) ? 1 : 0;
So, to adjust my code:
int nEstimateOfIntSize = (i<0?2:1)+(int)System.Math.Log10(i>0?i:-i);
and you have to do:
int nEstimateOfIntSize =11.
Krzemo
February 17th, 2005, 03:59 AM
and you have to do:
Code:
int nEstimateOfIntSize =11.
In general case - Yes:)
Yeah I know that loop was horrible but I was in a bit of a rush and not thinking.
He, he - too much cafee!
Wouldn't it be just as easy and efficient to do my original suggestion ?
efficient.... it depends if string parsing is made at compile or at run time.
I think it is worth to check it... but maybe later.
Best regards,
Krzemo.
cilu
February 17th, 2005, 04:45 AM
In general case - Yes
You have to take into account all case or you'll find yourself wondering why isn't the app working. ;)
Krzemo
February 17th, 2005, 05:04 AM
You have to take into account all case or you'll find yourself wondering why isn't the app working. ;)Sometimes, having negative value means that your application is already not working ;) .
But as I said before, it is irrelevant for stringbuilder (as far as performance is concern) if U use initial storage as 10,11,20 etc (of course if that value is small and U have enough memory available:) ). And if initial storage is too small, string buider alocates new: 2* size of storage size. So it will require low number of reallocations (geometrical speed). So really nothing happens when initial size is too small on the start ;).
Best regards,
Krzemo.
cilu
February 17th, 2005, 06:28 AM
But as I said before, it is irrelevant for stringbuilder (as far as performance is concern) if U use initial storage as 10,11,20 etc (of course if that value is small and U have enough memory available:) ). And if initial storage is too small, string buider alocates new: 2* size of storage size. So it will require low number of reallocations (geometrical speed). So really nothing happens when initial size is too small on the start ;).
I understand that. I was just saying that in general, all cases should be taken into account.
Issa
February 17th, 2005, 02:22 PM
hacky, but to clean it up wouldn't be in teh right spirit...the variable i is assumed to be any positive 32bit int.
i think this is pretty efficient, given that it avoids things such as hidden boxing when using object methods, and (as i understand it)
it pretty much does what stringbuilder does.
btw, how can one past code from VS and not have huge indentations such as these?
using System;
namespace CGF
{
class LabelText
{
public static void Main( )
{
char[] labelText = {
'u', ' ', 'h',
'a', 'v', 'e',
' ', 'c', 'h',
'o', 's', 'e',
'n', ' ', /*quote*/(char)0x0022,
/* spaces for 10 digits, max
* possible for System.Int32,
* also terminators for a string */
(char)0x0000, (char)0x0000, (char)0x0000,
(char)0x0000, (char)0x0000, (char)0x0000,
(char)0x0000, (char)0x0000, (char)0x0000,
(char)0x0000 };
// the is where we will start changing the array values
// based on teh user input
int QUOTE_POSITION = 15;
// offset the value of j/k by 48 to get the
// unicode value for the numeric character
*cp = (char)(48 + (j / k));
// remove the leading digit from j so we can
// get the next digit on the next pass
j = j - ((*(cp++)-48) * k);
n--;
}
*cp = (char)0x0022;
str = new string(carrPtr);
}
}
Console.WriteLine(str);
Console.Read();
}
}
}
Krzemo
February 17th, 2005, 02:58 PM
Wow !
U have definetely too much spare time :D .
it pretty much does what stringbuilder does.
No, but I think (in that particular case) your code can be the most eficient:thumb: solution (If its working ...I didn't compile it :rolleyes: ).
Best regards,
Krzemo.
cilu
February 17th, 2005, 04:09 PM
Oh my God! Somebody stop Issa until is too late! :D I'm too tired to try to undertand the code but I'll send you a bottle of champagne. My advice, get a life, get a job, stop writing Label classes. :D ;)
:wave:
Issa
February 17th, 2005, 05:00 PM
My advice, get a life, get a job, stop writing Label classes. :D ;)
:wave:
pff Label classes are my favourite! ;)
Zeb
February 17th, 2005, 05:29 PM
btw, how can one past code from VS and not have huge indentations such as these?
If you click "Tools" -> "Options" -> "Text Editor" -> "C#" -> "Tabs" and then select the "Insert Spaces" radio button, that will probably stop it. But then you get other problems when you are actually doing your coding because it stores spaces not tabs. and when you edit someones code who likes 4 spaces per tab and you like 3, it's a pain in the ***.
So do what I do - live with it! :D
and for the microseconds difference it's going to make doing it any other way, my votes on Darwens first post.
Krzemo
February 17th, 2005, 11:49 PM
Zeb:
my votes on Darwens first post. It is not a poll:D .
It is a competition: "Last One who provide valid solutions WINS! :D :D :D "
And last One is Issa solution - so he is the winner now!:cool:
and for the microseconds difference ... Did U measure it up or it is your internal believe?:p
Best regards,
Krzemo.
:wave:
Zeb
February 18th, 2005, 12:04 AM
Did U measure it up or it is your internal believe?:p
well, i coded em all up, then sat there with a stopwatch seeing which one went faster.
results were inconclusive.
khushi
February 19th, 2005, 01:18 AM
Hi,
All members
hey, u people had really enjoyed giving solutions of my question. isn't it. Anyway, thanks to all members u have given me no.of soutions of this particular problem.
Carry on. May be u people find more solutions.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.