Results 1 to 4 of 4

Thread: How to concatanate strings in C#

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    1

    Question How to concatanate strings in C#

    Dear, all

    I am new to programming as well as new to C# .
    Can somebody tell me how to concatanate strings in C#?
    Currently , I am building a calculator in VS 2005 and I am stuckup in
    decimal point button.

    Reagards,
    TR

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to concatanate strings in C#

    Welcome to the forums.

    There are a couple of ways to do that but I believe that using the StringBuilder Class is the best.

    Here is a pretty good article on using this class.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: How to concatanate strings in C#

    string msg = "hello " + "world";

    This is adequate for concatting 2 strings but as hack said if you are creating complicated and long strings it is best to use the stringbuilder. The reason being that strings are immutable, which means that a new string object is created each time you concatenate, this is not an issue when you are simply joining 2 or 3 strings (approximately the time it takes to instantiate a SB object.

    The rule of thumb is to use "x" + "y" for less than 3 or less strings and use a stringbuilder for 4 or more.
    I don't live here any more.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to concatanate strings in C#

    If you are concatenating several strings all at the one time I'd suggest using the String.Concat method. That way you're making a single method call rather than multiple calls to the Append method of a StringBuilder. If you're concatenating multiple strings in a loop or something like that, where you cannot do it all in a single line, then I'd go with the StringBuilder. Of course, if all you want to do is put a decimal point between two numbers then you can do something like:
    Code:
    string result = string.Format("{0}.{1}", wholePart, fractionalPart);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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