Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.
[Visual Basic] Public Sub CopyTo( _ ByVal sourceIndex As Integer, _ ByVal destination() As Char, _ ByVal destinationIndex As Integer, _ ByVal count As Integer _ ) [C#] public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count ); [C++] public: void CopyTo( int sourceIndex, __wchar_t destination __gc[], int destinationIndex, int count ); [JScript] public function CopyTo( sourceIndex : int, destination : Char[], destinationIndex : int, count : int );
| Exception Type | Condition |
|---|---|
| ArgumentNullException | destination is a null reference (Nothing in Visual Basic). |
| ArgumentOutOfRangeException | sourceIndex, destinationIndex, or count is negative
-or- count is greater than the length of the substring from startIndex to the end of this instance -or- count is greater than the length of the subarray from destinationIndex to the end of destination |
count characters are copied from the sourceIndex position of this instance to the destinationIndex position of destination.
sourceIndex and destinationIndex are zero-based.
The following code example demonstrates the CopyTo method.
[Visual Basic] Imports System Public Class CopyToTest Public Shared Sub Main() ' Embed an array of characters in a string Dim strSource As String = "changed" Dim destination As Char() = {"T"c, "h"c, "e"c, " "c, "i"c, "n"c, "i"c, _ "t"c, "i"c, "a"c, "l"c, " "c, "a"c, "r"c, "r"c, "a"c, "y"c} ' Print the char array Console.WriteLine(destination) ' Embed the source string in the destination string strSource.CopyTo(0, destination, 4, strSource.Length) ' Print the resulting array Console.WriteLine(destination) strSource = "A different string" ' Embed only a section of the source string in the destination strSource.CopyTo(2, destination, 3, 9) ' Print the resulting array Console.WriteLine(destination) End Sub 'Main End Class 'CopyToTest [C#] using System; public class CopyToTest { public static void Main() { // Embed an array of characters in a string string strSource = "changed"; char [] destination = { 'T', 'h', 'e', ' ', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ', 'a', 'r', 'r', 'a', 'y' }; // Print the char array Console.WriteLine( destination ); // Embed the source string in the destination string strSource.CopyTo ( 0, destination, 4, strSource.Length ); // Print the resulting array Console.WriteLine( destination ); strSource = "A different string"; // Embed only a section of the source string in the destination strSource.CopyTo ( 2, destination, 3, 9 ); // Print the resulting array Console.WriteLine( destination ); } } [C++] #using <mscorlib.dll> using namespace System; void main() { // Embed an array of characters in a string String* strSource = S"changed"; Char destination[] = { 'T', 'h', 'e', ' ', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ', 'a', 'r', 'r', 'a', 'y' }; // Print the char array Console::WriteLine(destination); // Embed the source string in the destination string strSource->CopyTo (0, destination, 4, strSource->Length); // Print the resulting array Console::WriteLine(destination); strSource = S"A different string"; // Embed only a section of the source string in the destination strSource->CopyTo (2, destination, 3, 9); // Print the resulting array Console::WriteLine(destination); } [JScript] import System; public class CopyToTest { public static function Main() : void { // Embed an array of characters in a string var strSource : String = "changed"; var destination : char[] = "The initial array".ToCharArray(); // Print the char array Console.WriteLine( destination ); // Embed the source string in the destination string strSource.CopyTo ( 0, destination, 4, strSource.Length ); // Print the resulting array Console.WriteLine( destination ); strSource = "A different string"; // Embed only a section of the source string in the destination strSource.CopyTo ( 2, destination, 3, 9 ); // Print the resulting array Console.WriteLine( destination ); } } CopyToTest.Main();
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
String Class | String Members | System Namespace | Char | Int32 | Insert | Substring