Concatenating Strings


You can concatenate strings and produce a new string from the combination. There are two ways to combine strings. One way is using a method called Concat in the string class. The other is to use the + or the += operator. The compiler then does the work of changing + and += to calls to the Concat function. In essence, there is one function to concatenate, and two ways of calling it.

To build one string from the combination of two or more strings:

  1. Declare a string variable to hold the result of the concatenation.

  2. Type

    = System.String.Concat(piece1,piece2); where piece1 and piece2 are either string literals or string variables ( Figure 4.20 ).

    Figure 4.20 The Concat method creates a new string object from two or more pieces.
     string firstName = "John Jacob "; string lastName = "Jingle Hymer Smith";  string name =   string.Concat(firstName,lastName);  Response.Write(name); //prints: John Jacob Jingle Hymer Smith 

    or

    Type = piece1 + piece2; where piece1 and piece2 are either string literals or string variables ( Figure 4.21 ).

    Figure 4.21 Concat is a method in System.String available to all languages, but the C# language also provides a quick way to call the Concat function: using the plus sign.
     string firstName = "John Jacob "; string lastName = "Jingle Hymer Smith";  string name = firstName + lastName;  Response.Write(name); //prints: John Jacob Jingle Hymer Smith 

    or

    Instead of declaring a new variable to hold the result you can use an existing variable and just append to it by typing piece1 += piece2 , where piece1 is the variable that contains the original string and piece2 is the variable you wish to append to the end of piece1 ( Figure 4.22 ).

    Figure 4.22 If you're used to programming in VBScript whenever you want to take an existing variable and append to it, you end up writing an expression like var1 = var1 + newvar. In C# the same expression can be compacted into var1 += newvar.
     string firstName = "John Jacob "; string lastName = "Jingle Hymer Smith";  firstName += lastName;  Response.Write(  firstName  ); //prints: John Jacob Jingle Hymer Smith 

graphics/tick.gif Tips

  • The Concat function is a static function. Static functions will be explained in more detail in Chapter 6, "Special Members." For now it's enough to know that to invoke a static function you don't need to create an instance of the class. You simply use the name of the class plus a dot plus the name of the function, as in the examples above.

  • There are nine versions of the Concat function. You can pass to the Concat function as many segments as you like. It's also possible to pass objects to the function that aren't strings. The Concat function will turn the objects into strings by calling the ToString function on each object. See "Representing Objects as Strings" later in this chapter ( Figure 4.23 ).

    Figure 4.23 The parameters for the Concat function are of type Object which means that you can pass in strings, numbers , instances of classes, etc.
     int x=5; string str = "a string";  string result =   string.Concat(str,x,txtUserID);  Response.Write(result); //prints out: a string5System.Web.UI- //.WebControls.TextBox //notice that txtUserID is a TextBox. //Calling ToString on a TextBox simply //returns the class name not the contents 
  • When you use the plus operator or the plus equal operator, the compiler turns the code into calls to the Concat function. The compiler is smart enough to know that if you glue three pieces together with a plus sign, it should use the version of the Concat function that accepts three parameters, instead of concatenating the first to the second and then concatenating the result to the third, which would be inefficient.

  • Remember that string buffers can't be changed, so when you concatenate to a string using any of the above mechanisms, the result is that the .NET Framework allocates a new string object in memory to store the characters from all the pieces in the string.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net