CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Join Date
    Dec 2004
    Location
    Delhi, India
    Posts
    59

    Smile 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?????

  2. #2
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: How to print "1" ?

    string str = " u have chosen \"" + i.ToString()+"\"";


  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: How to print "1" ?

    Code:
    int i = 1;
    label1.Text = string.Format("u have chosen \"{0}\"", i + 1);
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: How to print "1" ?

    Hey - let's see how many ways we can do this ? Hehe...

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Dec 2004
    Location
    Delhi, India
    Posts
    59

    Smile Re: How to print "1" ?

    Hi,

    Ajay Vijay

    solution which u have given is not working. Anyway, thanks. Now i got the solutions.

  7. #7
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: How to print "1" ?

    Quote 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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  8. #8
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    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.

  9. #9
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  10. #10
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  11. #11
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: How to print "1" ?

    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" ?

    And another thing - your solution cannot be counted in the competition "Last One who provide valid solutions WINS!" because it is not valid .


    IMHO this "string.Format("u have chosen \"{0}\"", i + 1);" will produce output like that:
    u have chosen "2"

    Best regards,
    Krzemo.

  12. #12
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  13. #13
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  14. #14
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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#?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  15. #15
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured