Section 16.3. String Constructors


16.3. String Constructors

Class String provides eight constructors for initializing Strings. Figure 16.1 demonstrates three of the constructors.

Figure 16.1. String constructors

  1  ' Fig. 16.1: StringConstructor.vb  2  ' Demonstrating String class constructors.  3  4  Module StringConstructor  5     Sub Main()  6        Dim originalString, string1, string2, _  7           string3, string4 As String  8        Dim characterArray() As Char = _  9           {"b"c, "i"c, "r"c, "t"c, "h"c, " "c, "d"c, "a"c, "y"c} 10 11        ' string initialization                       12        originalString = "Welcome to VB programming!" 13        string1 = originalString                      14        string2 = New String(characterArray)          15        string3 = New String(characterArray, 6, 3)    16        string4 = New String("C"c, 5)                 17 18        Console.WriteLine("string1 = " & """" & string1 & """" & _ 19           vbCrLf & "string2 = " & """" & string2 & """" & vbCrLf & _ 20           "string3 = " & """" & string3 & """" & vbCrLf & _ 21           "string4 = " & """" & string4 & """" & vbCrLf) 22     End Sub ' Main 23  End Module ' StringConstructor 

 string1 = "Welcome to VB programming!" string2 = "birth day" string3 = "day" string4 = "CCCCC" 



Lines 67 declare the String variables originalString, string1, string2, string3 and string4. Lines 89 allocate the Char array characterArray, which contains nine characters. Line 12 assigns the String literal "Welcome to VB programming!" to variable originalString. Line 13 sets string1 to refer to the same String literal as originalString.

Line 14 assigns a new String to string2, using the String constructor that takes a character array as an argument. The new String contains a copy of the characters in array characterArray.

Software Engineering Observation 16.1

In most cases, it is not necessary to make a copy of an existing String. All Strings are immutabletheir character contents cannot be changed after they are created. Also, if there are one or more references to a String (or any object, for that matter), the object cannot be reclaimed by the garbage collector. When a new value is assigned to a String variable, the variable simply refers to a different String object in memory.


Line 15 assigns a new String to string3, using the String constructor that takes a Char array and two Integer arguments. The second argument specifies the starting index position (the offset) from which characters in the array are to be copied. The third argument specifies the number of characters (the count) to be copied from the specified starting position in the array. The new String contains a copy of the specified characters in the array. If the offset or count causes the program to access an element outside the character array's bounds, an ArgumentOutOfRangeException is thrown.

Line 16 assigns a new String to string4, using the String constructor that takes as arguments a character and an Integer specifying the number of times to repeat that character in the String. Lines 1821 output the contents of variables string1, string2, string3 and string4.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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