string Indexer, Length Property and CopyTo Method

The application in Fig. 16.2 presents the string indexer, which facilitates the retrieval of any character in the string, and the string property Length, which returns the length of the string. The string method CopyTo copies a specified number of characters from a string into a char array.

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

1 // Fig. 16.2: StringMethods.cs 2 // Using the indexer, property Length and method CopyTo 3 // of class string. 4 using System; 5 6 class StringMethods 7 { 8 public static void Main() 9 { 10 string string1; 11 char[] characterArray; 12 13 string1 = "hello there"; 14 characterArray = new char[ 5 ]; 15 16 // output string1 17 Console.WriteLine( "string1: "" + string1 + """ ); 18 19 // test Length property 20 Console.WriteLine( "Length of string1: " + string1.Length ); 21 22 // loop through characters in string1 and display reversed 23 Console.Write( "The string reversed is: " ); 24 25 for ( int i = string1.Length - 1; i >= 0; i-- ) 26 Console.Write( string1[ i ] ); 27 28 // copy characters from string1 into characterArray 29 string1.CopyTo( 0, characterArray, 0, characterArray.Length ); 30 Console.Write( " The character array is: " ); 31 32 for ( int i = 0; i < characterArray.Length; i++ ) 33 Console.Write( characterArray[ i ] ); 34 35 Console.WriteLine( " " ); 36 } // end method Main 37 } // end class StringMethods

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

This application determines the length of a string, displays its characters in reverse order and copies a series of characters from the string to a character array.

Line 20 uses string property Length to determine the number of characters in string1. Like arrays, strings always know their own size.

Lines 2526 write the characters of string1 in reverse order using the string indexer. The string indexer treats a string as an array of chars and returns the character at a specific position in the string. The indexer receives an integer argument as the position number and returns the character at that position. As with arrays, the first element of a string is considered to be at position 0.

Common Programming Error 16 1

Attempting to access a character that is outside a string's bounds (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 string1 into a character array (characterArray). The first argument given to method CopyTo 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 3233 output the char array contents one character at a time.

Категории