16.4 string Indexer, Length Property and CopyTo Method

Figure 16.2 presents

  • the string indexer ([]) for retrieving any character in a string,

  • the string property Length, which returns a string’s length and

  • the string method CopyTo, which copies a specified number of characters from a string into a char array.

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

Alternate View

 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      static void Main()
 9      {
10         var string1 = "hello there";
11         var characterArray = new char[5];
12
13         Console.WriteLine($"string1: "{string1}""); // output string1
14
15         // test Length property
16         Console.WriteLine($"Length of string1: {string1.Length}");
17
18         // loop through characters in string1 and display reversed
19         Console.Write("The string reversed is: ");
20
21         for (int i = string1.Length - 1; i >= 0; --i)
22         {
23            Console.Write(string1[i]);
24         }
25
26         // copy characters from string1 into characterArray
27         string1.CopyTo(0, characterArray, 0, characterArray.Length);
28         Console.Write("
The character array is: ");
29
30         foreach (var element in characterArray)
31         {
32            Console.Write(element);
33         }
34
35         Console.WriteLine("
");
36      }
37   }

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

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

Lines 21–24 display 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. As with arrays, the first element of a string has index 0.

Common Programming Error 16.1

Attempting to access a character that’s outside a string’s bounds results in an Index-OutOfRangeException.

Line 27 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 30–33 output the char array contents one character at a time.

We used a for statement in lines 21–24 to demonstrate a string’s Length property and the string indexer, using them to display the string in reverse. That loop could have been implemented with foreach and the Reverse extension method as in


foreach (var element in string1.Reverse())
{
   Console.Write(element);
}

Method Reverse is one of many LINQ extension methods and requires a using directive for the namespace System.Linq.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset