16.8. Concatenating Strings Like the & operator, the String class's Shared method Concat (Fig. 16.7) can be used to concatenate two Strings. The method returns a new String containing the combined characters from both original Strings. Line 13 appends the characters from string2 to the end of a copy of string1, using method Concat. The original Strings are not modified. Figure 16.7. Concat Shared method. 1 ' Fig. 16.7: SubConcatenation.vb 2 ' Demonstrating String class Concat method. 3 4 Module StringConcatenation 5 Sub Main() 6 Dim string1 As String = "Happy " 7 Dim string2 As String = "Birthday" 8 9 Console.WriteLine("string1 = """ & string1 & """" & _ 10 vbCrLf & "string2 = """ & string2 & """") 11 Console.WriteLine(vbCrLf & _ 12 "Result of string.Concat( string1, string2 ) = " & _ 13 String.Concat(string1, string2)) 14 Console.WriteLine("string1 after concatenation = " & string1) 15 End Sub ' Main 16 End Module ' StringConcatenation [View full width] string1 = "Happy " string2 = "Birthday" Result of string.Concat( string1, string2 ) = Happy Birthday string1 after concatenation = Happy |
| |