Skip to main content
StringCopyTo Method
 

Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public void CopyTo(
	int sourceIndex,
	char[] destination,
	int destinationIndex,
	int count
)
public:
void CopyTo(
	int sourceIndex,
	array<wchar_t>^ destination,
	int destinationIndex,
	int count
)
member CopyTo : 
        sourceIndex:int *
        destination:char[] *
        destinationIndex:int *
        count:int -> unit
Public Sub CopyTo (
	sourceIndex As Integer,
	destination As Char(),
	destinationIndex As Integer,
	count As Integer
)

Parameters

sourceIndex
Type:

The index of the first character in this instance to copy.

destination
Type:

An array of Unicode characters to which characters in this instance are copied.

destinationIndex
Type:

The index in destination at which the copy operation begins.

count
Type:

The number of characters in this instance to copy to destination.

Exception Condition
ArgumentNullException

destination is null.

ArgumentOutOfRangeException

sourceIndex, destinationIndex, or count is negative

-or-

sourceIndex does not identify a position in the current instance.

-or-

destinationIndex does not identify a valid index in the destination array.

-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 the destination array.

This method copies count characters from the sourceIndex position of this instance to the destinationIndex position of destination character array. This method does not resize the destination character array; it must have a sufficient number of elements to accommodate the copied characters or the method throws an ArgumentOutOfRangeException.

sourceIndex and destinationIndex are zero-based.

The following example demonstrates the CopyTo method.

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 );
    }
}
// The example displays the following output:
//       The initial array
//       The changed array
//       Thedifferentarray
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 
End Class 
' The example displays the following output:
'       The initial array
'       The changed array
'       Thedifferentarray
using namespace System;
int main()
{

   // Embed an array of characters in a string
   String^ strSource = "changed";
   array<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 );
}
// The example displays the following output:
//       The initial array
//       The changed array
//       Thedifferentarray
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top