Concatenating strings
The + operator (discussed in Chapter 3, Introduction to C# Programming) is not the only way to perform string concatenation. The static method Concat of class string (Fig. 16.7) concatenates two strings and returns a new string containing the combined characters from both original strings. Line 16 appends the characters from string2 to the end of a copy of string1, using method Concat. The statement in line 16 does not modify the original strings.
Figure 16.7. Concat static method.
(This item is displayed on page 778 in the print version)
1 // Fig. 16.7: SubConcatenation.cs 2 // Demonstrating string class Concat method. 3 using System; 4 5 class StringConcatenation 6 { 7 public static void Main() 8 { 9 string string1 = "Happy "; 10 string string2 = "Birthday"; 11 12 Console.WriteLine( "string1 = "" + string1 + "" " + 13 "string2 = "" + string2 + """ ); 14 Console.WriteLine( 15 " Result of string.Concat( string1, string2 ) = " + 16 string.Concat( string1, string2 ) ); 17 Console.WriteLine( "string1 after concatenation = " + string1 ); 18 } // end method Main 19 } // end class StringConcatenation
|
Miscellaneous string Methods
|