Section 16.4. String Indexer, Length Property and CopyTo Method


16.4. String Indexer, Length Property and CopyTo Method

Figure 16.2 uses the String indexer to access individual characters in a String, and demonstrates String property Length, which returns the length of the String. The program also uses String method CopyTo to copy a specified number of characters from a String into a Char array. This application determines the length of a String, reverses the order of the characters in the String and copies a series of characters from the String to a character array.

Figure 16.2. String indexer, Length property and CopyTo method.

  1  ' Fig. 16.2: StringMethods.vb  2  ' Using the indexer, property Length and method CopyTo  3  ' of class String.  4  5  Module StringMethods  6     Sub Main()  7        Dim string1 As String  8        Dim characterArray() As Char  9 10        string1 = "hello there" 11        characterArray = New Char (5) {} 12 13        ' output string1 14        Console.WriteLine("string1: """ & string1 & """") 15 16        ' test Length property                                    17        Console.WriteLine("Length of string1: " & string1.Length) 18 19        ' loop through characters in string1 and display reversed 20        Console.Write("The string reversed is: ") 21 22        Dim i As Integer 23 24        For i = string1.Length - 1 To 0 Step -1 25           Console.Write(string1(i)) 26        Next i 27 28        ' copy characters from string1 into characterArray 29        string1.CopyTo(0, characterArray, 0, 5)            30        Console.Write(vbCrLf & "The character array is: ") 31 32        For i = 0 To characterArray.Length - 1 33           Console.Write(characterArray(i)) 34        Next i 35 36        Console.WriteLine(vbCrLf) 37     End Sub ' Main 38  End Module ' StringMethods 

 string1: "hello there" Length of string1: 11 The string reversed is: ereht olleh The character array is: hello 



Line 17 uses String property Length to determine the number of characters in string1. Like arrays, Strings always know their own size.

Lines 2426 output the characters of string1 in reverse order using the String indexer, which treats a String as an array of Chars and returns the character at a specific index in the String. The indexer receives an integer argument as the index and returns the character at that index. As with arrays, the first element of a String is considered to be at position 0.

Common Programming Error 16.2

Attempting to access a character that is outside the bounds of a String (i.e., an index less than 0 or an index greater than or equal to the String's length) results in an IndexOutOfRangeException.


Line 29 uses String method CopyTo to copy the characters of a String (string1) into a character array (characterArray). CopyTo's first argument is the index from which the method begins copying characters in the String. The second argument is the character array into which the characters are copied. The third argument is the index specifying the starting location at which the method begins placing the copied characters into the character array. The last argument is the number of characters that the method will copy from the String. Lines 3234 output the Char array contents one character at a time.



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