Skip to main content
.NET Framework Class Library
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)
Syntax
Public Sub CopyTo ( _
	sourceIndex As [%$TOPIC/w97h5wh7_en-us_VS_110_1_0_0_0_0%], _
	destination As [%$TOPIC/w97h5wh7_en-us_VS_110_1_0_0_0_1%](), _
	destinationIndex As [%$TOPIC/w97h5wh7_en-us_VS_110_1_0_0_0_2%], _
	count As [%$TOPIC/w97h5wh7_en-us_VS_110_1_0_0_0_3%] _
)
public void CopyTo(
	[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_1_0_0%] sourceIndex,
	[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_1_0_1%][] destination,
	[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_1_0_2%] destinationIndex,
	[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_1_0_3%] count
)
public:
void CopyTo(
	[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_2_0_0%] sourceIndex, 
	array<[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_2_0_1%]>^ destination, 
	[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_2_0_2%] destinationIndex, 
	[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_2_0_3%] count
)
member CopyTo : 
        sourceIndex:[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_3_0_0%] * 
        destination:[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_3_0_1%][] * 
        destinationIndex:[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_3_0_2%] * 
        count:[%$TOPIC/w97h5wh7_en-us_VS_110_1_0_3_0_3%] -> unit

Parameters

sourceIndex
Type: SystemInt32

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

destination
Type: SystemChar

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

destinationIndex
Type: SystemInt32

The index in destination at which the copy operation begins.

count
Type: SystemInt32

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

Exceptions
ExceptionCondition
ArgumentNullException

destination is .

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.

Remarks

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.

Examples

The following example demonstrates the CopyTo method.

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 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
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
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.