- Preparing Your Machine to Work with Strings
- Working with Strings
- Initializing Strings
- Comparing Strings
- Concatenating Strings
- Finding the String Length
- Comparing and Concatenating Strings in the Sample Application
- Creating Strings from Characters
- Using Escape Characters
- Using Literal Strings
- Accessing the String's Characters
- Finding a Substring within a String
- Extracting Part of the String
- Splitting a String
- Joining a String
- Uppercasing and Lowercasing
- Formatting Strings
- Finishing the Sample Application
- Representing Objects as Strings
- Allocating Strings with StringBuilder
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:
Declare a string variable to hold the result of the concatenation.
= 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.
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.
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.
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.
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.